home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / coff-ppc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  95.7 KB  |  3,299 lines

  1. /* BFD back-end for PowerPC Microsoft Portable Executable files.
  2.    Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  3.  
  4.    Original version pieced together by Kim Knuttila (krk@cygnus.com)
  5.  
  6.    There is nothing new under the sun. This file draws a lot on other
  7.    coff files, in particular, those for the rs/6000, alpha, mips, and 
  8.    intel backends, and the PE work for the arm.
  9.  
  10. This file is part of BFD, the Binary File Descriptor library.
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  25.  
  26. /* Current State:
  27.    - objdump works
  28.    - relocs generated by gas
  29.    - ld will link files, but they do not run.
  30.    - dlltool will not produce correct output in some .reloc cases, and will 
  31.      not produce the right glue code for dll function calls.
  32. */
  33.  
  34.  
  35. #include "bfd.h"
  36. #include "sysdep.h"
  37.  
  38. #include "libbfd.h"
  39. #include "obstack.h"
  40.  
  41. #include "coff/powerpc.h"
  42. #include "coff/internal.h"
  43.  
  44. #include "coff/pe.h"
  45.  
  46. #ifdef BADMAG
  47. #undef BADMAG
  48. #endif
  49.  
  50. #define BADMAG(x) PPCBADMAG(x)
  51.  
  52. #include "libcoff.h"
  53.  
  54. /* The toc is a set of bfd_vma fields. We use the fact that valid         */
  55. /* addresses are even (i.e. the bit representing "1" is off) to allow     */
  56. /* us to encode a little extra information in the field                   */
  57. /* - Unallocated addresses are intialized to 1.                           */
  58. /* - Allocated addresses are even numbers.                                */
  59. /* The first time we actually write a reference to the toc in the bfd,    */
  60. /* we want to record that fact in a fixup file (if it is asked for), so   */
  61. /* we keep track of whether or not an address has been written by marking */
  62. /* the low order bit with a "1" upon writing                              */
  63.  
  64. #define SET_UNALLOCATED(x)  ((x) = 1)
  65. #define IS_UNALLOCATED(x)   ((x) == 1)
  66.  
  67. #define IS_WRITTEN(x)       ((x) & 1)
  68. #define MARK_AS_WRITTEN(x)  ((x) |= 1)
  69. #define MAKE_ADDR_AGAIN(x)  ((x) &= ~1)
  70.  
  71. /* In order not to add an int to every hash table item for every coff
  72.    linker, we define our own hash table, derived from the coff one */
  73.  
  74. /* PE linker hash table entries. */
  75.  
  76. struct ppc_coff_link_hash_entry
  77. {
  78.   struct coff_link_hash_entry root; /* First entry, as required  */
  79.  
  80.   /* As we wonder around the relocs, we'll keep the assigned toc_offset
  81.      here */
  82.   bfd_vma toc_offset;               /* Our addition, as required */
  83.   int symbol_is_glue;
  84.   unsigned long int glue_insn;
  85.   char eye_catcher[8];
  86. };
  87.  
  88. /* Need a 7 char string for an eye catcher */
  89. #define EYE "krkjunk"
  90.  
  91. #define CHECK_EYE(addr) \
  92.  if (strcmp(addr, EYE) != 0) \
  93.   { \
  94.     fprintf(stderr,\
  95.     "File %s, line %d, Hash check failure, bad eye %8s\n", \
  96.     __FILE__, __LINE__, addr); \
  97.     abort(); \
  98.  }
  99.  
  100. /* PE linker hash table.  */
  101.  
  102. struct ppc_coff_link_hash_table
  103. {
  104.   struct coff_link_hash_table root; /* First entry, as required */
  105. };
  106.  
  107. static struct bfd_hash_entry *ppc_coff_link_hash_newfunc
  108.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
  109.        const char *));
  110.  
  111. /* Routine to create an entry in the link hash table.  */
  112.  
  113. static struct bfd_hash_entry *
  114. ppc_coff_link_hash_newfunc (entry, table, string)
  115.      struct bfd_hash_entry *entry;
  116.      struct bfd_hash_table *table;
  117.      const char *string;
  118. {
  119.   struct ppc_coff_link_hash_entry *ret = 
  120.     (struct ppc_coff_link_hash_entry *) entry;
  121.  
  122.   /* Allocate the structure if it has not already been allocated by a
  123.      subclass.  */
  124.   if (ret == (struct ppc_coff_link_hash_entry *) NULL)
  125.     ret = (struct ppc_coff_link_hash_entry *)
  126.       bfd_hash_allocate (table, 
  127.              sizeof (struct ppc_coff_link_hash_entry));
  128.  
  129.   if (ret == (struct ppc_coff_link_hash_entry *) NULL)
  130.     return NULL;
  131.  
  132.   /* Call the allocation method of the superclass.  */
  133.   ret = ((struct ppc_coff_link_hash_entry *)
  134.      _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret, 
  135.                       table, string));
  136.  
  137.   if (ret)
  138.     {
  139.       /* Initialize the local fields.  */
  140.       SET_UNALLOCATED(ret->toc_offset);
  141.       ret->symbol_is_glue = 0;
  142.       ret->glue_insn = 0;
  143.       strcpy(ret->eye_catcher, EYE);
  144.     }
  145.  
  146.   return (struct bfd_hash_entry *) ret;
  147. }
  148.  
  149. /* Initialize a PE linker hash table.  */
  150.  
  151. static boolean
  152. ppc_coff_link_hash_table_init (table, abfd, newfunc)
  153.      struct ppc_coff_link_hash_table *table;
  154.      bfd *abfd;
  155.      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
  156.                         struct bfd_hash_table *,
  157.                         const char *));
  158. {
  159.   return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc);
  160. }
  161.  
  162. /* Create a PE linker hash table.  */
  163.  
  164. static struct bfd_link_hash_table *
  165. ppc_coff_link_hash_table_create (abfd)
  166.      bfd *abfd;
  167. {
  168.   struct ppc_coff_link_hash_table *ret;
  169.  
  170.   ret = ((struct ppc_coff_link_hash_table *)
  171.      bfd_alloc (abfd, sizeof (struct ppc_coff_link_hash_table)));
  172.   if (ret == NULL)
  173.     return NULL;
  174.   if (! ppc_coff_link_hash_table_init (ret, abfd,
  175.                     ppc_coff_link_hash_newfunc))
  176.     {
  177.       bfd_release (abfd, ret);
  178.       return (struct bfd_link_hash_table *) NULL;
  179.     }
  180.   return &ret->root.root;
  181. }
  182.  
  183. /* Now, tailor coffcode.h to use our hash stuff */
  184.  
  185. #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
  186.  
  187.  
  188. /* The nt loader points the toc register to &toc + 32768, in order to */
  189. /* use the complete range of a 16-bit displacement (I guess). We have */
  190. /* to adjust for this when we fix up loads displaced off the toc reg. */
  191. #define TOC_LOAD_ADJUSTMENT (-32768)
  192. #define TOC_SECTION_NAME ".private.toc"
  193.  
  194. /* The main body of code is in coffcode.h.  */
  195.  
  196. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
  197.  
  198. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
  199.    from smaller values.  Start with zero, widen, *then* decrement.  */
  200. #define MINUS_ONE    (((bfd_vma)0) - 1)
  201.  
  202. /* these should definitely go in a header file somewhere... */
  203.  
  204. /* NOP */
  205. #define IMAGE_REL_PPC_ABSOLUTE          0x0000
  206.  
  207. /* 64-bit address */
  208. #define IMAGE_REL_PPC_ADDR64            0x0001
  209.  
  210. /* 32-bit address */
  211. #define IMAGE_REL_PPC_ADDR32            0x0002
  212.  
  213. /* 26-bit address, shifted left 2 (branch absolute) */
  214. #define IMAGE_REL_PPC_ADDR24            0x0003
  215.  
  216. /* 16-bit address */
  217. #define IMAGE_REL_PPC_ADDR16            0x0004
  218.  
  219. /* 16-bit address, shifted left 2 (load doubleword) */
  220. #define IMAGE_REL_PPC_ADDR14            0x0005
  221.  
  222. /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
  223. #define IMAGE_REL_PPC_REL24             0x0006
  224.  
  225. /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
  226. #define IMAGE_REL_PPC_REL14             0x0007
  227.  
  228. /* 16-bit offset from TOC base */
  229. #define IMAGE_REL_PPC_TOCREL16          0x0008
  230.  
  231. /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
  232. #define IMAGE_REL_PPC_TOCREL14          0x0009
  233.  
  234. /* 32-bit addr w/o image base */
  235. #define IMAGE_REL_PPC_ADDR32NB          0x000A
  236.  
  237. /* va of containing section (as in an image sectionhdr) */
  238. #define IMAGE_REL_PPC_SECREL            0x000B
  239.  
  240. /* sectionheader number */
  241. #define IMAGE_REL_PPC_SECTION           0x000C
  242.  
  243. /* substitute TOC restore instruction iff symbol is glue code */
  244. #define IMAGE_REL_PPC_IFGLUE            0x000D
  245.  
  246. /* symbol is glue code; virtual address is TOC restore instruction */
  247. #define IMAGE_REL_PPC_IMGLUE            0x000E
  248.  
  249. /* va of containing section (limited to 16 bits) */
  250. #define IMAGE_REL_PPC_SECREL16          0x000F
  251.  
  252. /* stuff to handle immediate data when the number of bits in the */
  253. /* data is greater than the number of bits in the immediate field */
  254. /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
  255. #define IMAGE_REL_PPC_REFHI             0x0010
  256. #define IMAGE_REL_PPC_REFLO             0x0011
  257. #define IMAGE_REL_PPC_PAIR              0x0012
  258.  
  259. /* This is essentially the same as tocrel16, with TOCDEFN assumed */
  260. #define IMAGE_REL_PPC_TOCREL16_DEFN     0x0013
  261.  
  262. /*  Flag bits in IMAGE_RELOCATION.TYPE */
  263.  
  264. /* subtract reloc value rather than adding it */
  265. #define IMAGE_REL_PPC_NEG               0x0100
  266.  
  267. /* fix branch prediction bit to predict branch taken */
  268. #define IMAGE_REL_PPC_BRTAKEN           0x0200
  269.  
  270. /* fix branch prediction bit to predict branch not taken */
  271. #define IMAGE_REL_PPC_BRNTAKEN          0x0400
  272.  
  273. /* toc slot defined in file (or, data in toc) */
  274. #define IMAGE_REL_PPC_TOCDEFN           0x0800
  275.  
  276. /* masks to isolate above values in IMAGE_RELOCATION.Type */
  277. #define IMAGE_REL_PPC_TYPEMASK          0x00FF
  278. #define IMAGE_REL_PPC_FLAGMASK          0x0F00
  279.  
  280. #define EXTRACT_TYPE(x)                 ((x) & IMAGE_REL_PPC_TYPEMASK)
  281. #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
  282. #define EXTRACT_JUNK(x)  \
  283.            ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
  284.  
  285.  
  286. /* static helper functions to make relocation work */
  287. /* (Work In Progress) */
  288.  
  289. static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd,
  290.                               arelent *reloc,
  291.                               asymbol *symbol,
  292.                               PTR data,
  293.                               asection *section,
  294.                               bfd *output_bfd,
  295.                               char **error));
  296. #if 0
  297. static bfd_reloc_status_type ppc_reflo_reloc PARAMS ((bfd *abfd,
  298.                               arelent *reloc,
  299.                               asymbol *symbol,
  300.                               PTR data,
  301.                               asection *section,
  302.                               bfd *output_bfd,
  303.                               char **error));
  304. #endif
  305. static bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd,
  306.                              arelent *reloc,
  307.                              asymbol *symbol,
  308.                              PTR data,
  309.                              asection *section,
  310.                              bfd *output_bfd,
  311.                              char **error));
  312.  
  313.  
  314. static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd,
  315.                               arelent *reloc,
  316.                               asymbol *symbol,
  317.                               PTR data,
  318.                               asection *section,
  319.                               bfd *output_bfd,
  320.                               char **error));
  321.  
  322. #if 0
  323. static bfd_reloc_status_type ppc_addr32nb_reloc PARAMS ((bfd *abfd,
  324.                              arelent *reloc,
  325.                              asymbol *symbol,
  326.                              PTR data,
  327.                              asection *section,
  328.                              bfd *output_bfd,
  329.                              char **error));
  330. #endif
  331. static bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd,
  332.                             arelent *reloc,
  333.                             asymbol *symbol,
  334.                             PTR data,
  335.                             asection *section,
  336.                             bfd *output_bfd,
  337.                             char **error));
  338.  
  339. static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd,
  340.                                arelent *reloc,
  341.                                asymbol *symbol,
  342.                                PTR data,
  343.                                asection *section,
  344.                                bfd *output_bfd,
  345.                                char **error));
  346.  
  347. static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd,
  348.                                arelent *reloc,
  349.                                asymbol *symbol,
  350.                                PTR data,
  351.                                asection *section,
  352.                                bfd *output_bfd,
  353.                                char **error));
  354.  
  355.  
  356.  
  357. static boolean in_reloc_p PARAMS((bfd *abfd, reloc_howto_type *howto));
  358.  
  359.  
  360. /* FIXME: It'll take a while to get through all of these. I only need a few to
  361.    get us started, so those I'll make sure work. Those marked FIXME are either
  362.    completely unverified or have a specific unknown marked in the comment */
  363.  
  364. /*---------------------------------------------------------------------------*/
  365. /*                                                                           */
  366. /* Relocation entries for Windows/NT on PowerPC.                             */
  367. /*                                                                           */
  368. /* From the document "" we find the following listed as used relocs:         */
  369. /*                                                                           */
  370. /*   ABSOLUTE       : The noop                                               */
  371. /*   ADDR[64|32|16] : fields that hold addresses in data fields or the       */
  372. /*                    16 bit displacement field on a load/store.             */
  373. /*   ADDR[24|14]    : fields that hold addresses in branch and cond          */
  374. /*                    branches. These represent [26|16] bit addresses.       */
  375. /*                    The low order 2 bits are preserved.                    */
  376. /*   REL[24|14]     : branches relative to the Instruction Address           */
  377. /*                    register. These represent [26|16] bit addresses,       */
  378. /*                    as before. The instruction field will be zero, and     */
  379. /*                    the address of the SYM will be inserted at link time.  */
  380. /*   TOCREL16       : 16 bit displacement field referring to a slot in       */
  381. /*                    toc.                                                   */
  382. /*   TOCREL14       : 16 bit displacement field, similar to REL14 or ADDR14. */
  383. /*   ADDR32NB       : 32 bit address relative to the virtual origin.         */
  384. /*                    (On the alpha, this is always a linker generated thunk)*/
  385. /*                    (i.e. 32bit addr relative to the image base)           */
  386. /*   SECREL         : The value is relative to the start of the section      */
  387. /*                    containing the symbol.                                 */
  388. /*   SECTION        : access to the header containing the item. Supports the */
  389. /*                    codeview debugger.                                     */
  390. /*                                                                           */
  391. /* In particular, note that the document does not indicate that the          */
  392. /* relocations listed in the header file are used.                           */
  393. /*                                                                           */
  394. /*                                                                           */
  395. /*                                                                           */
  396. /*---------------------------------------------------------------------------*/
  397.  
  398. static reloc_howto_type ppc_coff_howto_table[] =
  399. {
  400.   /* IMAGE_REL_PPC_ABSOLUTE 0x0000   NOP */
  401.   /* Unused: */
  402.   HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */                                 
  403.      0,                     /* rightshift */                           
  404.      0,                     /* size (0 = byte, 1 = short, 2 = long) */ 
  405.      0,                     /* bitsize */                   
  406.      false,                     /* pc_relative */                          
  407.      0,                     /* bitpos */                               
  408.      complain_overflow_dont, /* dont complain_on_overflow */
  409.      0,                 /* special_function */                     
  410.      "ABSOLUTE",             /* name */
  411.      false,                     /* partial_inplace */                      
  412.      0x00,                  /* src_mask */                             
  413.      0x00,                 /* dst_mask */                             
  414.      false),                 /* pcrel_offset */
  415.   
  416.   /* IMAGE_REL_PPC_ADDR64 0x0001  64-bit address */
  417.   /* Unused: */
  418.   HOWTO(IMAGE_REL_PPC_ADDR64,    /* type */                                 
  419.     0,                     /* rightshift */                           
  420.     3,                     /* size (0 = byte, 1 = short, 2 = long) */ 
  421.     64,                     /* bitsize */                   
  422.     false,                     /* pc_relative */                          
  423.     0,                     /* bitpos */                               
  424.     complain_overflow_bitfield,      /* complain_on_overflow */
  425.     0,                 /* special_function */                     
  426.     "ADDR64",               /* name */
  427.     true,                     /* partial_inplace */                      
  428.     MINUS_ONE,          /* src_mask */
  429.     MINUS_ONE,             /* dst_mask */
  430.     false),                 /* pcrel_offset */
  431.  
  432.   /* IMAGE_REL_PPC_ADDR32 0x0002  32-bit address */
  433.   /* Used: */
  434.   HOWTO (IMAGE_REL_PPC_ADDR32,    /* type */
  435.      0,                    /* rightshift */                           
  436.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  437.      32,                    /* bitsize */                   
  438.      false,                    /* pc_relative */                          
  439.      0,                    /* bitpos */                               
  440.      complain_overflow_bitfield, /* complain_on_overflow */
  441.      0,                /* special_function */                     
  442.      "ADDR32",              /* name */
  443.      true,                    /* partial_inplace */                      
  444.      0xffffffff,            /* src_mask */                             
  445.      0xffffffff,            /* dst_mask */                             
  446.      false),                /* pcrel_offset */
  447.   
  448.   /* IMAGE_REL_PPC_ADDR24 0x0003  26-bit address, shifted left 2 (branch absolute) */
  449.   /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
  450.   /* Of course, That's the IBM approved bit numbering, which is not what */
  451.   /* anyone else uses.... The li field is in bit 2 thru 25 */ 
  452.   /* Used: */
  453.   HOWTO (IMAGE_REL_PPC_ADDR24,  /* type */
  454.      0,                    /* rightshift */                           
  455.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  456.      26,                    /* bitsize */
  457.      false,                    /* pc_relative */                          
  458.      0,                    /* bitpos */                               
  459.      complain_overflow_bitfield, /* complain_on_overflow */
  460.      0,                /* special_function */                     
  461.      "ADDR24",              /* name */
  462.      true,                    /* partial_inplace */                      
  463.      0x07fffffc,            /* src_mask */                             
  464.      0x07fffffc,            /* dst_mask */                             
  465.      false),                /* pcrel_offset */
  466.   
  467.   /* IMAGE_REL_PPC_ADDR16 0x0004  16-bit address */
  468.   /* Used: */
  469.   HOWTO (IMAGE_REL_PPC_ADDR16,  /* type */             
  470.      0,                    /* rightshift */                           
  471.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  472.      16,                    /* bitsize */                   
  473.      false,                    /* pc_relative */                          
  474.      0,                    /* bitpos */                               
  475.      complain_overflow_signed, /* complain_on_overflow */
  476.      0,                /* special_function */                     
  477.      "ADDR16",              /* name */
  478.      true,                    /* partial_inplace */                      
  479.      0xffff,            /* src_mask */                             
  480.      0xffff,            /* dst_mask */                             
  481.      false),                /* pcrel_offset */
  482.   
  483.   /* IMAGE_REL_PPC_ADDR14 0x0005 */
  484.   /*  16-bit address, shifted left 2 (load doubleword) */
  485.   /* FIXME: the mask is likely wrong, and the bit position may be as well */
  486.   /* Unused: */
  487.   HOWTO (IMAGE_REL_PPC_ADDR14,  /* type */             
  488.      1,                    /* rightshift */                           
  489.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  490.      16,                    /* bitsize */                   
  491.      false,                    /* pc_relative */                          
  492.      0,                    /* bitpos */                               
  493.      complain_overflow_signed, /* complain_on_overflow */
  494.      0,                /* special_function */                     
  495.      "ADDR16",              /* name */
  496.      true,                    /* partial_inplace */                      
  497.      0xffff,            /* src_mask */                             
  498.      0xffff,            /* dst_mask */                             
  499.      false),                /* pcrel_offset */
  500.   
  501.   /* IMAGE_REL_PPC_REL24 0x0006 */
  502.   /*   26-bit PC-relative offset, shifted left 2 (branch relative) */
  503.   /* Used: */
  504.   HOWTO (IMAGE_REL_PPC_REL24,   /* type */
  505.      0,                    /* rightshift */                           
  506.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  507.      26,                    /* bitsize */                   
  508.      true,                    /* pc_relative */                          
  509.      0,                    /* bitpos */                               
  510.      complain_overflow_signed, /* complain_on_overflow */
  511.      0,                /* special_function */                     
  512.      "REL24",               /* name */
  513.      true,                    /* partial_inplace */                      
  514.      0x3fffffc,            /* src_mask */                             
  515.      0x3fffffc,            /* dst_mask */                             
  516.      false),                /* pcrel_offset */
  517.   
  518.   /* IMAGE_REL_PPC_REL14 0x0007 */
  519.   /*   16-bit PC-relative offset, shifted left 2 (br cond relative) */
  520.   /* FIXME: the mask is likely wrong, and the bit position may be as well */
  521.   /* FIXME: how does it know how far to shift? */
  522.   /* Unused: */
  523.   HOWTO (IMAGE_REL_PPC_ADDR14,  /* type */             
  524.      1,                    /* rightshift */                           
  525.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  526.      16,                    /* bitsize */                   
  527.      false,                    /* pc_relative */                          
  528.      0,                    /* bitpos */                               
  529.      complain_overflow_signed, /* complain_on_overflow */
  530.      0,                /* special_function */                     
  531.      "ADDR16",              /* name */
  532.      true,                    /* partial_inplace */                      
  533.      0xffff,            /* src_mask */                             
  534.      0xffff,            /* dst_mask */                             
  535.      true),                 /* pcrel_offset */
  536.   
  537.   /* IMAGE_REL_PPC_TOCREL16 0x0008 */
  538.   /*   16-bit offset from TOC base */
  539.   /* Used: */
  540.   HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */             
  541.      0,                    /* rightshift */                           
  542.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  543.      16,                    /* bitsize */                   
  544.      false,                    /* pc_relative */                          
  545.      0,                    /* bitpos */                               
  546.      complain_overflow_dont, /* complain_on_overflow */
  547.      ppc_toc16_reloc,       /* special_function */                     
  548.      "TOCREL16",            /* name */
  549.      false,                    /* partial_inplace */                      
  550.      0xffff,            /* src_mask */                             
  551.      0xffff,            /* dst_mask */                             
  552.      false),                /* pcrel_offset */
  553.   
  554.   /* IMAGE_REL_PPC_TOCREL14 0x0009 */
  555.   /*   16-bit offset from TOC base, shifted left 2 (load doubleword) */
  556.   /* Unused: */
  557.   HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */             
  558.      1,                    /* rightshift */                           
  559.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  560.      16,                    /* bitsize */                   
  561.      false,                    /* pc_relative */                          
  562.      0,                    /* bitpos */                               
  563.      complain_overflow_signed, /* complain_on_overflow */
  564.      0,                /* special_function */                     
  565.      "TOCREL14",            /* name */
  566.      false,                    /* partial_inplace */                      
  567.      0xffff,            /* src_mask */                             
  568.      0xffff,            /* dst_mask */                             
  569.      false),                /* pcrel_offset */
  570.   
  571.   /* IMAGE_REL_PPC_ADDR32NB 0x000A */
  572.   /*   32-bit addr w/ image base */
  573.   /* Unused: */
  574.   HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */             
  575.      0,                    /* rightshift */                           
  576.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  577.      32,                    /* bitsize */                   
  578.      false,                    /* pc_relative */                          
  579.      0,                    /* bitpos */                               
  580.      complain_overflow_signed, /* complain_on_overflow */
  581.      0,                     /* special_function */                     
  582.      "ADDR32NB",            /* name */
  583.      true,                    /* partial_inplace */                      
  584.      0xffffffff,            /* src_mask */                             
  585.      0xffffffff,            /* dst_mask */                             
  586.      false),                 /* pcrel_offset */
  587.   
  588.   /* IMAGE_REL_PPC_SECREL 0x000B */
  589.   /*   va of containing section (as in an image sectionhdr) */
  590.   /* Unused: */
  591.   HOWTO (IMAGE_REL_PPC_SECREL,/* type */             
  592.      0,                    /* rightshift */                           
  593.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  594.      32,                    /* bitsize */                   
  595.      false,                    /* pc_relative */                          
  596.      0,                    /* bitpos */                               
  597.      complain_overflow_signed, /* complain_on_overflow */
  598.      ppc_secrel_reloc,      /* special_function */                     
  599.      "SECREL",              /* name */
  600.      true,                    /* partial_inplace */                      
  601.      0xffffffff,            /* src_mask */                             
  602.      0xffffffff,            /* dst_mask */                             
  603.      true),                 /* pcrel_offset */
  604.  
  605.   /* IMAGE_REL_PPC_SECTION 0x000C */
  606.   /*   sectionheader number */
  607.   /* Unused: */
  608.   HOWTO (IMAGE_REL_PPC_SECTION,/* type */             
  609.      0,                    /* rightshift */                           
  610.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  611.      32,                    /* bitsize */                   
  612.      false,                    /* pc_relative */                          
  613.      0,                    /* bitpos */                               
  614.      complain_overflow_signed, /* complain_on_overflow */
  615.      ppc_section_reloc,     /* special_function */                     
  616.      "SECTION",             /* name */
  617.      true,                    /* partial_inplace */                      
  618.      0xffffffff,            /* src_mask */                             
  619.      0xffffffff,            /* dst_mask */                             
  620.      true),                 /* pcrel_offset */
  621.  
  622.   /* IMAGE_REL_PPC_IFGLUE 0x000D */
  623.   /*   substitute TOC restore instruction iff symbol is glue code */
  624.   /* Used: */
  625.   HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */             
  626.      0,                    /* rightshift */                           
  627.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  628.      32,                    /* bitsize */                   
  629.      false,                    /* pc_relative */                          
  630.      0,                    /* bitpos */                               
  631.      complain_overflow_signed, /* complain_on_overflow */
  632.      0,                /* special_function */                     
  633.      "IFGLUE",              /* name */
  634.      true,                    /* partial_inplace */                      
  635.      0xffffffff,            /* src_mask */                             
  636.      0xffffffff,            /* dst_mask */                             
  637.      false),                /* pcrel_offset */
  638.  
  639.   /* IMAGE_REL_PPC_IMGLUE 0x000E */
  640.   /*   symbol is glue code; virtual address is TOC restore instruction */
  641.   /* Unused: */
  642.   HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */             
  643.      0,                    /* rightshift */                           
  644.      2,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  645.      32,                    /* bitsize */                   
  646.      false,                    /* pc_relative */                          
  647.      0,                    /* bitpos */                               
  648.      complain_overflow_dont, /* complain_on_overflow */
  649.      ppc_imglue_reloc,      /* special_function */                     
  650.      "IMGLUE",              /* name */
  651.      false,                    /* partial_inplace */                      
  652.      0xffffffff,            /* src_mask */                             
  653.      0xffffffff,            /* dst_mask */                             
  654.      false),                 /* pcrel_offset */
  655.  
  656.   /* IMAGE_REL_PPC_SECREL16 0x000F */
  657.   /*   va of containing section (limited to 16 bits) */
  658.   /* Unused: */
  659.   HOWTO (IMAGE_REL_PPC_SECREL16,/* type */             
  660.      0,                    /* rightshift */                           
  661.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  662.      16,                    /* bitsize */                   
  663.      false,                    /* pc_relative */                          
  664.      0,                    /* bitpos */                               
  665.      complain_overflow_signed, /* complain_on_overflow */
  666.      0,                /* special_function */                     
  667.      "SECREL16",            /* name */
  668.      true,                    /* partial_inplace */                      
  669.      0xffff,            /* src_mask */                             
  670.      0xffff,            /* dst_mask */                             
  671.      true),                 /* pcrel_offset */
  672.  
  673.   /* IMAGE_REL_PPC_REFHI             0x0010 */
  674.   /* Unused: */
  675.   HOWTO (IMAGE_REL_PPC_REFHI,   /* type */             
  676.      0,                    /* rightshift */                           
  677.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  678.      16,                    /* bitsize */                   
  679.      false,                    /* pc_relative */                          
  680.      0,                    /* bitpos */                               
  681.      complain_overflow_signed, /* complain_on_overflow */
  682.      ppc_refhi_reloc,    /* special_function */                     
  683.      "REFHI",               /* name */
  684.      true,                    /* partial_inplace */                      
  685.      0xffffffff,            /* src_mask */                             
  686.      0xffffffff,            /* dst_mask */                             
  687.      false),                 /* pcrel_offset */
  688.  
  689.   /* IMAGE_REL_PPC_REFLO             0x0011 */
  690.   /* Unused: */
  691.   HOWTO (IMAGE_REL_PPC_REFLO,   /* type */             
  692.      0,                    /* rightshift */                           
  693.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  694.      16,                    /* bitsize */                   
  695.      false,                    /* pc_relative */                          
  696.      0,                    /* bitpos */                               
  697.      complain_overflow_signed, /* complain_on_overflow */
  698.      ppc_refhi_reloc,    /* special_function */                     
  699.      "REFLO",               /* name */
  700.      true,                    /* partial_inplace */                      
  701.      0xffffffff,            /* src_mask */                             
  702.      0xffffffff,            /* dst_mask */                             
  703.      false),                /* pcrel_offset */
  704.  
  705.   /* IMAGE_REL_PPC_PAIR              0x0012 */
  706.   /* Unused: */
  707.   HOWTO (IMAGE_REL_PPC_PAIR,    /* type */             
  708.      0,                    /* rightshift */                           
  709.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  710.      16,                    /* bitsize */                   
  711.      false,                    /* pc_relative */                          
  712.      0,                    /* bitpos */                               
  713.      complain_overflow_signed, /* complain_on_overflow */
  714.      ppc_pair_reloc,        /* special_function */                     
  715.      "PAIR",                /* name */
  716.      true,                    /* partial_inplace */                      
  717.      0xffffffff,            /* src_mask */                             
  718.      0xffffffff,            /* dst_mask */                             
  719.      false),                /* pcrel_offset */
  720.  
  721.   /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
  722.   /*   16-bit offset from TOC base, without causing a definition */
  723.   /* Used: */
  724.   HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */ 
  725.      0,                    /* rightshift */                           
  726.      1,                    /* size (0 = byte, 1 = short, 2 = long) */ 
  727.      16,                    /* bitsize */                   
  728.      false,                    /* pc_relative */                          
  729.      0,                    /* bitpos */                               
  730.      complain_overflow_dont, /* complain_on_overflow */
  731.      0,                     /* special_function */                     
  732.      "TOCREL16, TOCDEFN",   /* name */
  733.      false,                    /* partial_inplace */                      
  734.      0xffff,            /* src_mask */                             
  735.      0xffff,            /* dst_mask */                             
  736.      false),                /* pcrel_offset */
  737.  
  738. };
  739.  
  740.  
  741.  
  742.  
  743. /* Some really cheezy macros that can be turned on to test stderr :-) */
  744.  
  745. #ifdef DEBUG_RELOC
  746. #define UN_IMPL(x)                                           \
  747. {                                                            \
  748.    static int i;                                             \
  749.    if (i == 0)                                               \
  750.      {                                                       \
  751.        i = 1;                                                \
  752.        fprintf(stderr,"Unimplemented Relocation -- %s\n",x); \
  753.      }                                                       \
  754. }
  755.  
  756. #define DUMP_RELOC(n,r)                              \
  757. {                                                    \
  758.    fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
  759.        n, (*(r->sym_ptr_ptr))->name,             \
  760.        r->address, r->addend);                   \
  761. }
  762.  
  763. /* Given a reloc name, n, and a pointer to an internal_reloc, 
  764.    dump out interesting information on the contents 
  765.  
  766. #define n_name        _n._n_name
  767. #define n_zeroes    _n._n_n._n_zeroes
  768. #define n_offset    _n._n_n._n_offset
  769.  
  770. */
  771.  
  772. #define DUMP_RELOC2(n,r)                     \
  773. {                                            \
  774.    fprintf(stderr,"%s sym %d, r_vaddr %d %s\n", \
  775.        n, r->r_symndx, r->r_vaddr,\
  776.        (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
  777.        ?" ":" TOCDEFN"  );      \
  778. }
  779.  
  780. #else
  781. #define UN_IMPL(x)
  782. #define DUMP_RELOC(n,r)
  783. #define DUMP_RELOC2(n,r)
  784. #endif
  785.  
  786.  
  787.  
  788. /* toc construction and management routines */
  789. extern bfd* bfd_of_toc_owner;
  790. extern long int global_toc_size;
  791.  
  792. extern long int import_table_size;
  793. extern long int first_thunk_address;
  794. extern long int thunk_size;
  795.  
  796. enum toc_type
  797. {
  798.   default_toc,
  799.   toc_32,
  800.   toc_64
  801. };
  802.  
  803. enum ref_category
  804. {
  805.   priv,
  806.   pub,
  807.   data
  808. };
  809.  
  810. struct list_ele
  811. {
  812.   struct list_ele *next;
  813.   bfd_vma addr;
  814.   enum ref_category cat;
  815.   int offset;
  816.   const char *name;
  817. };
  818.  
  819. extern struct list_ele *head;
  820. extern struct list_ele *tail;
  821.  
  822. static void
  823. record_toc(toc_section, our_toc_offset, cat, name)
  824.      asection *toc_section;
  825.      int our_toc_offset;
  826.      enum ref_category cat;
  827.      const char *name;
  828. {
  829.   /* add this entry to our toc addr-offset-name list */
  830.   struct list_ele *t;
  831.   t = (struct list_ele *) bfd_malloc (sizeof (struct list_ele));
  832.   if (t == NULL)
  833.     abort ();
  834.   t->next = 0;
  835.   t->offset = our_toc_offset;
  836.   t->name = name;
  837.   t->cat = cat;
  838.   t->addr = toc_section->output_offset + our_toc_offset;
  839.  
  840.   if (head == 0)
  841.     {
  842.       head = t;
  843.       tail = t;
  844.     }
  845.   else
  846.     {
  847.       tail->next = t;
  848.       tail = t;
  849.     }
  850. }
  851.  
  852. #ifdef COFF_IMAGE_WITH_PE
  853.  
  854. /* record a toc offset against a symbol */
  855. static int
  856. ppc_record_toc_entry(abfd, info, sec, sym, toc_kind)
  857.      bfd *abfd;
  858.      struct bfd_link_info *info;
  859.      asection *sec;
  860.      int sym;
  861.      enum toc_type toc_kind;
  862. {
  863.   struct ppc_coff_link_hash_entry *h;
  864.   int ret_val;
  865.   const char *name;
  866.  
  867.   int *local_syms;
  868.  
  869.   h = 0;
  870.  
  871.   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
  872.   if (h != 0)
  873.     {
  874.       CHECK_EYE(h->eye_catcher);
  875.     }
  876.  
  877.   if (h == 0) 
  878.     { 
  879.       local_syms = obj_coff_local_toc_table(abfd);
  880.       if (local_syms == 0)
  881.     {
  882.       int i;
  883.       /* allocate a table */
  884.       local_syms = 
  885.         (int *) bfd_zalloc (abfd, 
  886.                 obj_raw_syment_count(abfd) * sizeof(int));
  887.       if (local_syms == 0)
  888.         return false;
  889.       obj_coff_local_toc_table(abfd) = local_syms;
  890.       for (i = 0; i < obj_raw_syment_count(abfd); ++i)
  891.         {
  892.           SET_UNALLOCATED(local_syms[i]);
  893.         }
  894.     }
  895.  
  896.       if (IS_UNALLOCATED(local_syms[sym])) 
  897.     {
  898.       local_syms[sym] = global_toc_size;
  899.       ret_val = global_toc_size;
  900.       global_toc_size += 4;
  901.  
  902.       /* The size must fit in a 16bit displacment */
  903.       if (global_toc_size >= 65535)
  904.         {
  905.           fprintf(stderr,
  906.               "Exceeded toc size of 65535\n");
  907.           abort();
  908.         }
  909.  
  910. #ifdef TOC_DEBUG
  911.       fprintf(stderr,
  912.           "Setting toc_offset for local sym %d to %d\n",
  913.           sym, ret_val);
  914. #endif
  915.     }
  916.       else
  917.     {
  918.       ret_val = local_syms[sym];
  919. #ifdef TOC_DEBUG
  920.       fprintf(stderr,
  921.           "toc_offset already set for local sym %d to %d\n",
  922.           sym, ret_val);
  923. #endif
  924.     }
  925.     }
  926.   else
  927.     {
  928.       name = h->root.root.root.string;
  929.  
  930.       /* check to see if there's a toc slot allocated. If not, do it
  931.      here. It will be used in relocate_section */
  932.       if (IS_UNALLOCATED(h->toc_offset))
  933.     {
  934.       h->toc_offset = global_toc_size;
  935.       ret_val = global_toc_size;
  936.       global_toc_size += 4;
  937.  
  938.       /* The size must fit in a 16bit displacment */
  939.       if (global_toc_size >= 65535)
  940.         {
  941.           fprintf(stderr,
  942.               "Exceeded toc size of 65535\n");
  943.           abort();
  944.         }
  945.  
  946. #ifdef TOC_DEBUG
  947.       fprintf(stderr,
  948.           "Setting toc_offset for sym %d (%s) [h=%p] to %d\n",
  949.           sym, name, h, ret_val);
  950. #endif
  951.     }
  952.       else
  953.     {
  954.       ret_val = h->toc_offset;
  955. #ifdef TOC_DEBUG
  956.       fprintf(stderr,
  957.           "toc_offset already set for sym %d (%s) [h=%p] to %d\n",
  958.           sym, name, h, ret_val);
  959. #endif
  960.     }
  961.     }
  962.  
  963.   return ret_val;
  964. }
  965.  
  966. #endif /* COFF_IMAGE_WITH_PE */
  967.  
  968. #if 0
  969.  
  970. /* FIXME: record a toc offset against a data-in-toc symbol */
  971. /* Now, there is currenly some confusion on what this means. In some 
  972.    compilers one sees the moral equivalent of:
  973.       .tocd
  974.       define some data
  975.       .text
  976.       refer to the data with a [tocv] qualifier
  977.    In general, one sees something to indicate that a tocd has been
  978.    seen, and that would trigger the allocation of data in toc. The IBM
  979.    docs seem to suggest that anything with the TOCDEFN qualifier should
  980.    never trigger storage allocation. However, in the kernel32.lib that 
  981.    we've been using for our test bed, there are a couple of variables
  982.    referenced that fail that test.
  983.  
  984.    So it can't work that way.
  985. */
  986. static int
  987. ppc_record_data_in_toc_entry(abfd, info, sec, sym, toc_kind)
  988.      bfd *abfd;
  989.      struct bfd_link_info *info;
  990.      asection *sec;
  991.      int sym;
  992.      enum toc_type toc_kind;
  993. {
  994.   struct ppc_coff_link_hash_entry *h = 0;
  995.   int ret_val;
  996.   const char *name;
  997.  
  998.   int *local_syms;
  999.  
  1000.   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
  1001.  
  1002.   if (h == 0) 
  1003.     { 
  1004.       local_syms = obj_coff_local_toc_table(abfd);
  1005.       if (local_syms == 0)
  1006.     {
  1007.       int i;
  1008.       /* allocate a table */
  1009.       local_syms = 
  1010.         (int *) bfd_zalloc (abfd, 
  1011.                 obj_raw_syment_count(abfd) * sizeof(int));
  1012.       if (local_syms == 0)
  1013.         return false;
  1014.       obj_coff_local_toc_table(abfd) = local_syms;
  1015.       for (i = 0; i < obj_raw_syment_count(abfd); ++i)
  1016.         {
  1017.           SET_UNALLOCATED(local_syms[i]);
  1018.         }
  1019.     }
  1020.  
  1021.       if (IS_UNALLOCATED(local_syms[sym])) 
  1022.     {
  1023.       local_syms[sym] = global_toc_size;
  1024.       ret_val = global_toc_size;
  1025.       global_toc_size += 4;
  1026. #ifdef TOC_DEBUG
  1027.       fprintf(stderr,
  1028.           "Setting data_in_toc_offset for local sym %d to %d\n",
  1029.           sym, ret_val);
  1030. #endif
  1031.     }
  1032.       else
  1033.     {
  1034.       ret_val = local_syms[sym];
  1035. #ifdef TOC_DEBUG
  1036.       fprintf(stderr,
  1037.           "data_in_toc_offset already set for local sym %d to %d\n",
  1038.           sym, ret_val);
  1039. #endif
  1040.     }
  1041.     }
  1042.   else
  1043.     {
  1044.       CHECK_EYE(h->eye_catcher);
  1045.  
  1046.       name = h->root.root.root.string;
  1047.  
  1048.       /* check to see if there's a toc slot allocated. If not, do it
  1049.      here. It will be used in relocate_section */
  1050.       if (IS_UNALLOCATED(h->toc_offset))
  1051.     {
  1052. #if 0
  1053.       h->toc_offset = global_toc_size;
  1054. #endif
  1055.       ret_val = global_toc_size;
  1056.       /* We're allocating a chunk of the toc, as opposed to a slot */
  1057.       /* FIXME: alignment? */
  1058.       
  1059.       global_toc_size += 4;
  1060. #ifdef TOC_DEBUG
  1061.       fprintf(stderr,
  1062.           "Setting data_in_toc_offset for sym %d (%s) [h=%p] to %d\n",
  1063.           sym, name, h, ret_val);
  1064. #endif
  1065.     }
  1066.       else
  1067.     {
  1068.       ret_val = h->toc_offset;
  1069. #ifdef TOC_DEBUG
  1070.       fprintf(stderr,
  1071.           "data_in_toc_offset already set for sym %d (%s) [h=%p] to %d\n",
  1072.           sym, name, h, ret_val);
  1073. #endif
  1074.     }
  1075.     }
  1076.  
  1077.   return ret_val;
  1078. }
  1079.  
  1080. #endif /* 0 */
  1081.  
  1082. #ifdef COFF_IMAGE_WITH_PE
  1083.  
  1084. /* record a toc offset against a symbol */
  1085. static void
  1086. ppc_mark_symbol_as_glue(abfd, sym, rel)
  1087.      bfd *abfd;
  1088.      int sym;
  1089.      struct internal_reloc *rel;
  1090. {
  1091.   struct ppc_coff_link_hash_entry *h;
  1092.  
  1093.   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
  1094. #ifdef DEBUG_RELOC
  1095.   fprintf(stderr,
  1096.       "ppc_mark_symbol_as_glue:\n");
  1097. #endif
  1098.   CHECK_EYE(h->eye_catcher);
  1099.  
  1100.   h->symbol_is_glue = 1;
  1101.   h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
  1102.  
  1103.   return;
  1104. }
  1105.  
  1106. #endif /* COFF_IMAGE_WITH_PE */
  1107.  
  1108. #if 0
  1109.  
  1110. /* Provided the symbol, returns the value reffed */
  1111. static long get_symbol_value PARAMS ((asymbol *));
  1112.  
  1113. static long
  1114. get_symbol_value (symbol)       
  1115.      asymbol *symbol;
  1116. {                                             
  1117.   long relocation = 0;
  1118.  
  1119.   if (bfd_is_com_section (symbol->section))
  1120.     {
  1121.       relocation = 0;                           
  1122.     }
  1123.   else 
  1124.     {                                      
  1125.       relocation = symbol->value +
  1126.     symbol->section->output_section->vma +
  1127.       symbol->section->output_offset;
  1128.     }                                           
  1129.  
  1130.   return(relocation);
  1131. }
  1132.  
  1133. #endif /* 0 */
  1134.  
  1135. /* Return true if this relocation should
  1136.    appear in the output .reloc section. */
  1137.  
  1138. static boolean in_reloc_p(abfd, howto)
  1139.      bfd * abfd;
  1140.      reloc_howto_type *howto;
  1141. {
  1142.   return 
  1143.     (! howto->pc_relative) 
  1144.       && (howto->type != IMAGE_REL_PPC_ADDR32NB)
  1145.       && (howto->type != IMAGE_REL_PPC_TOCREL16)
  1146.       && (howto->type != IMAGE_REL_PPC_IMGLUE)
  1147.       && (howto->type != IMAGE_REL_PPC_IFGLUE) 
  1148.       && (howto->type != IMAGE_REL_PPC_SECREL)
  1149.       && (howto->type != IMAGE_REL_PPC_SECTION)
  1150.       && (howto->type != IMAGE_REL_PPC_SECREL16)
  1151.       && (howto->type != IMAGE_REL_PPC_REFHI)
  1152.       && (howto->type != IMAGE_REL_PPC_REFLO)
  1153.       && (howto->type != IMAGE_REL_PPC_PAIR)
  1154.       && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
  1155. }     
  1156.  
  1157. #if 0
  1158.  
  1159. /* this function is in charge of performing all the ppc PE relocations */
  1160. /* Don't yet know if we want to do this this particular way ... (krk)  */
  1161. /* FIXME: (it is not yet enabled) */
  1162.  
  1163. static bfd_reloc_status_type
  1164. pe_ppc_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
  1165.           error_message)
  1166.      bfd *abfd;
  1167.      arelent *reloc_entry;
  1168.      asymbol *symbol_in;
  1169.      PTR data;
  1170.      asection *input_section;
  1171.      bfd *output_bfd;
  1172.      char **error_message;
  1173. {
  1174.   /* the consth relocation comes in two parts, we have to remember
  1175.      the state between calls, in these variables */
  1176.   static boolean part1_consth_active = false;
  1177.   static unsigned long part1_consth_value;
  1178.  
  1179.   unsigned long sym_value;
  1180.   unsigned short r_type;
  1181.   unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
  1182.     
  1183.   fprintf(stderr, "pe_ppc_reloc (%s)\n", TARGET_LITTLE_NAME);
  1184.  
  1185.   r_type = reloc_entry->howto->type;
  1186.  
  1187.   if (output_bfd) 
  1188.     {
  1189.       /* Partial linking - do nothing */
  1190.       reloc_entry->address += input_section->output_offset;
  1191.       return bfd_reloc_ok; 
  1192.     }
  1193.  
  1194.   if (symbol_in != NULL
  1195.       && bfd_is_und_section (symbol_in->section))
  1196.     {
  1197.       /* Keep the state machine happy in case we're called again */
  1198.       if (r_type == IMAGE_REL_PPC_REFHI) 
  1199.     {
  1200.       part1_consth_active = true;
  1201.       part1_consth_value  = 0;
  1202.     }
  1203.       return(bfd_reloc_undefined);
  1204.     }
  1205.   
  1206.   if ((part1_consth_active) && (r_type != IMAGE_REL_PPC_PAIR)) 
  1207.     {
  1208.       part1_consth_active = false;
  1209.       *error_message = (char *) "Missing PAIR";
  1210.       return(bfd_reloc_dangerous);
  1211.     }
  1212.  
  1213.  
  1214.   sym_value = get_symbol_value(symbol_in);
  1215.   
  1216.   return(bfd_reloc_ok);    
  1217. }
  1218.  
  1219. #endif /* 0 */
  1220.  
  1221. /* The reloc processing routine for the optimized COFF linker.  */
  1222.  
  1223. static boolean
  1224. coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
  1225.                contents, relocs, syms, sections)
  1226.      bfd *output_bfd;
  1227.      struct bfd_link_info *info;
  1228.      bfd *input_bfd;
  1229.      asection *input_section;
  1230.      bfd_byte *contents;
  1231.      struct internal_reloc *relocs;
  1232.      struct internal_syment *syms;
  1233.      asection **sections;
  1234. {
  1235.   struct internal_reloc *rel;
  1236.   struct internal_reloc *relend;
  1237.   boolean hihalf;
  1238.   bfd_vma hihalf_val;
  1239.   asection *toc_section = 0;
  1240.   bfd_vma relocation;
  1241.   reloc_howto_type *howto = 0;
  1242.   
  1243. #ifdef DEBUG_RELOC
  1244.   fprintf(stderr, 
  1245.       "pe_ppc_relocate_section (%s) for %s in bfd %s\n", 
  1246.       TARGET_LITTLE_NAME,
  1247.       input_section->name,
  1248.       input_bfd->filename);
  1249.   
  1250. #endif  
  1251.  
  1252.   /* If we are performing a relocateable link, we don't need to do a
  1253.      thing.  The caller will take care of adjusting the reloc
  1254.      addresses and symbol indices.  */
  1255.   if (info->relocateable)
  1256.     return true;
  1257.   
  1258.   hihalf = false;
  1259.   hihalf_val = 0;
  1260.  
  1261.   rel = relocs;
  1262.   relend = rel + input_section->reloc_count;
  1263.   for (; rel < relend; rel++)
  1264.     {
  1265.       long symndx;
  1266.       struct ppc_coff_link_hash_entry *h;
  1267.       struct internal_syment *sym;
  1268.       bfd_vma val;
  1269.  
  1270.       asection *sec;
  1271.       bfd_reloc_status_type rstat;
  1272.       bfd_byte *loc;
  1273.  
  1274.       unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
  1275.       unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
  1276.   
  1277. #ifdef DEBUG_RELOC
  1278.       /* now examine flags */
  1279.       if (r_flags != 0) 
  1280.     {
  1281.       fprintf (stderr, "Reloc with flags found!");
  1282.       if ( r_flags & IMAGE_REL_PPC_NEG ) 
  1283.         fprintf (stderr, " NEG");
  1284.       if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
  1285.         fprintf (stderr, " BRTAKEN");
  1286.       if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
  1287.         fprintf (stderr, " BRNTAKEN");
  1288.       if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
  1289.         fprintf (stderr, " TOCDEFN");
  1290.       fprintf(stderr, "\n");
  1291.     }
  1292. #endif
  1293.  
  1294.       symndx = rel->r_symndx;
  1295.       loc = contents + rel->r_vaddr - input_section->vma;
  1296.  
  1297.       /* FIXME: check bounds on r_type */
  1298.       howto = ppc_coff_howto_table + r_type;
  1299.  
  1300.       if (symndx == -1)
  1301.     {
  1302.       h = NULL;
  1303.       sym = NULL;
  1304.     }
  1305.       else
  1306.     {
  1307.       h = (struct ppc_coff_link_hash_entry *) 
  1308.         (obj_coff_sym_hashes (input_bfd)[symndx]);
  1309.       if (h != 0) 
  1310.         {
  1311.           CHECK_EYE(h->eye_catcher);
  1312.         }
  1313.  
  1314.       sym = syms + symndx;
  1315.     }
  1316.  
  1317.       if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
  1318.     {
  1319.       fprintf(stderr,
  1320.           "relocate_section: IMGLUE reloc has no name!\n");
  1321.       abort();
  1322.     }
  1323.  
  1324.       sec = NULL;
  1325.       val = 0;
  1326.  
  1327.       /* FIXME: PAIR unsupported in the following code */
  1328.       if (h == NULL)
  1329.     {
  1330.       if (symndx == -1)
  1331.         sec = bfd_abs_section_ptr;
  1332.       else
  1333.         {
  1334.           sec = sections[symndx];
  1335.           val = (sec->output_section->vma
  1336.              + sec->output_offset
  1337.              + sym->n_value
  1338.              - sec->vma);
  1339.         }
  1340.     }
  1341.       else
  1342.     {
  1343.       CHECK_EYE(h->eye_catcher);
  1344.  
  1345.       if (h->root.root.type == bfd_link_hash_defined
  1346.           || h->root.root.type == bfd_link_hash_defweak)
  1347.         {
  1348.           sec = h->root.root.u.def.section;
  1349.           val = (h->root.root.u.def.value
  1350.              + sec->output_section->vma
  1351.              + sec->output_offset);
  1352.         }
  1353.       else
  1354.         {
  1355.           fprintf(stderr,
  1356.               "missing %s\n",h->root.root.root.string);
  1357.           if (! ((*info->callbacks->undefined_symbol)
  1358.              (info, h->root.root.root.string, input_bfd, input_section,
  1359.               rel->r_vaddr - input_section->vma)))
  1360.         return false;
  1361.         }
  1362.     }
  1363.  
  1364.       rstat = bfd_reloc_ok;
  1365.       
  1366.       /* Each case must do its own relocation, setting rstat appropriately */
  1367.       switch (r_type)
  1368.     {
  1369.     default:
  1370.       fprintf( stderr, 
  1371.           "ERROR: during reloc processing -- unsupported reloc %s\n", 
  1372.           howto->name);
  1373.       bfd_set_error (bfd_error_bad_value);
  1374.       abort();
  1375.       return false;
  1376.     case IMAGE_REL_PPC_TOCREL16:
  1377.       {
  1378.         bfd_vma our_toc_offset;
  1379.         int fixit;
  1380.  
  1381.         DUMP_RELOC2(howto->name, rel);
  1382.  
  1383.         if (toc_section == 0) 
  1384.           {
  1385.         toc_section = bfd_get_section_by_name (bfd_of_toc_owner, 
  1386.                                TOC_SECTION_NAME);
  1387. #ifdef TOC_DEBUG
  1388.  
  1389.         fprintf(stderr,
  1390.             "BFD of toc owner %p (%s), section addr of %s %p\n",
  1391.              bfd_of_toc_owner, bfd_of_toc_owner->filename, 
  1392.             TOC_SECTION_NAME, toc_section);
  1393. #endif
  1394.  
  1395.         if ( toc_section == NULL ) 
  1396.           {
  1397.             fprintf(stderr, "No Toc section!\n");
  1398.             abort();
  1399.           }
  1400.           }
  1401.  
  1402.         /* 
  1403.          *  Amazing bit tricks present. As we may have seen earlier, we
  1404.          *  use the 1 bit to tell us whether or not a toc offset has been
  1405.          *  allocated. Now that they've all been allocated, we will use
  1406.          *  the 1 bit to tell us if we've written this particular toc
  1407.          *  entry out.
  1408.          */
  1409.         fixit = false;
  1410.         if (h == 0)
  1411.           { /* it is a file local symbol */
  1412.         int *local_toc_table;
  1413.         const char *name;
  1414.  
  1415.         sym = syms + symndx;
  1416.         name = sym->_n._n_name;
  1417.  
  1418.         local_toc_table = obj_coff_local_toc_table(input_bfd);
  1419.         our_toc_offset = local_toc_table[symndx];
  1420.  
  1421.         if (IS_WRITTEN(our_toc_offset))
  1422.           {
  1423.             /* if it has been written out, it is marked with the 
  1424.                1 bit. Fix up our offset, but do not write it out
  1425.                again.
  1426.              */
  1427.             MAKE_ADDR_AGAIN(our_toc_offset);
  1428. #ifdef TOC_DEBUG
  1429.  
  1430.             fprintf(stderr,
  1431.                 "Not writing out toc_offset of %d for %s\n", 
  1432.                 our_toc_offset, name);
  1433. #endif
  1434.           }
  1435.         else
  1436.           {
  1437.             /* write out the toc entry */
  1438.             record_toc(toc_section, our_toc_offset, priv, strdup(name));
  1439. #ifdef TOC_DEBUG
  1440.             fprintf(stderr,
  1441.                 "Writing out toc_offset "
  1442.                 "toc_section (%p,%p)+%d val %d for %s\n", 
  1443.                 toc_section,
  1444.                 toc_section->contents,
  1445.                 our_toc_offset, 
  1446.                 val,
  1447.                 name);
  1448. #endif
  1449.  
  1450.             bfd_put_32(output_bfd,
  1451.                    val,
  1452.                    toc_section->contents + our_toc_offset);
  1453.  
  1454.             MARK_AS_WRITTEN(local_toc_table[symndx]);
  1455.             fixit = true;
  1456.           }
  1457.           }
  1458.         else
  1459.           {
  1460.         const char *name = h->root.root.root.string;
  1461.         our_toc_offset = h->toc_offset;
  1462.  
  1463.         if ((r_flags & IMAGE_REL_PPC_TOCDEFN) 
  1464.             == IMAGE_REL_PPC_TOCDEFN )
  1465.           {
  1466.             /* This is unbelievable cheese. Some knowledgable asm 
  1467.                hacker has decided to use r2 as a base for loading 
  1468.                a value. He/She does this by setting the tocdefn bit, 
  1469.                and not supplying a toc definition. The behaviour is 
  1470.                then to use the difference between the value of the 
  1471.                symbol and the actual location of the toc as the toc 
  1472.                index. 
  1473.  
  1474.                In fact, what is usually happening is, because the
  1475.                Import Address Table is mapped immediately following
  1476.                the toc, some trippy library code trying for speed on
  1477.                dll linkage, takes advantage of that and considers 
  1478.                the IAT to be part of the toc, thus saving a load.
  1479.             */
  1480. #ifdef DEBUG_RELOC
  1481.             fprintf(stderr,
  1482.  "TOCDEFN is on, (%s) (%p) our_toc_offset = %x, val (%x) vma (%x) off (%x)\n", 
  1483.                 name, h, our_toc_offset,
  1484.                 val, toc_section->output_section->vma, 
  1485.                 toc_section->output_offset);
  1486. #endif
  1487.  
  1488.             our_toc_offset = val - 
  1489.               (toc_section->output_section->vma + 
  1490.                toc_section->output_offset);
  1491.  
  1492. #ifdef DEBUG_RELOC
  1493.             fprintf(stderr,
  1494.                 "               our_toc_offset set to %x\n", our_toc_offset);
  1495. #endif
  1496.  
  1497.             /* The size must still fit in a 16bit displacment */
  1498.             if (our_toc_offset >= 65535)
  1499.               {
  1500.             fprintf(stderr,
  1501.                 "Error: TOCDEFN Relocation of %d for %s exceeded displacement of 65535\n", our_toc_offset, name);
  1502.             abort();
  1503.               }
  1504.  
  1505.             record_toc(toc_section, our_toc_offset, pub, strdup(name));
  1506.           }
  1507.         else if (IS_WRITTEN(our_toc_offset))
  1508.           {
  1509.             /* if it has been written out, it is marked with the 
  1510.                1 bit. Fix up our offset, but do not write it out
  1511.                again.
  1512.              */
  1513.             MAKE_ADDR_AGAIN(our_toc_offset);
  1514. #ifdef TOC_DEBUG
  1515.             fprintf(stderr,
  1516.                 "Not writing out toc_offset of %d for %s\n", 
  1517.                 our_toc_offset, name);
  1518. #endif
  1519.           }
  1520.         else
  1521.           {
  1522.             record_toc(toc_section, our_toc_offset, pub, strdup(name));
  1523.  
  1524. #ifdef TOC_DEBUG
  1525.             /* write out the toc entry */
  1526.             fprintf(stderr,
  1527.                 "Writing out toc_offset "
  1528.                 "toc_section (%p,%p)+%d val %d for %s\n", 
  1529.                 toc_section,
  1530.                 toc_section->contents,
  1531.                 our_toc_offset, 
  1532.                 val,
  1533.                 name);
  1534. #endif
  1535.  
  1536.             /* write out the toc entry */
  1537.             bfd_put_32(output_bfd,
  1538.                    val,
  1539.                    toc_section->contents + our_toc_offset);
  1540.  
  1541.             MARK_AS_WRITTEN(h->toc_offset);
  1542.             /* The tricky part is that this is the address that */
  1543.             /* needs a .reloc entry for it */
  1544.             fixit = true;
  1545.           }
  1546.           }
  1547.  
  1548.         if (fixit && info->base_file) 
  1549.           {
  1550.         /* So if this is non pcrelative, and is referenced
  1551.            to a section or a common symbol, then it needs a reloc */
  1552.  
  1553.         /* relocation to a symbol in a section which
  1554.            isn't absolute - we output the address here 
  1555.            to a file */
  1556.  
  1557.         bfd_vma addr =  toc_section->output_section->vma
  1558.           + toc_section->output_offset + our_toc_offset;
  1559.             
  1560.         if (coff_data(output_bfd)->pe)
  1561.           addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
  1562.  
  1563. #ifdef DEBUG_RELOC
  1564.         fprintf(stderr,
  1565.             "  Toc Section .reloc candidate addr = %x\n", addr);
  1566. #endif
  1567.         fwrite (&addr, 1,4, (FILE *) info->base_file);
  1568.           }
  1569.  
  1570.  
  1571.         /* FIXME: this test is conservative */
  1572.         if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN &&
  1573.         our_toc_offset > toc_section->_raw_size)
  1574.           {
  1575.         fprintf(stderr,
  1576.             "reloc offset is bigger than the toc size!\n");
  1577.         abort();
  1578.           }
  1579.  
  1580.         /* Now we know the relocation for this toc reference */
  1581.         relocation =  our_toc_offset + TOC_LOAD_ADJUSTMENT;
  1582.         rstat = _bfd_relocate_contents (howto,
  1583.                         input_bfd, 
  1584.                         relocation, 
  1585.                         loc);
  1586.       }
  1587.       break;
  1588.     case IMAGE_REL_PPC_IFGLUE:
  1589.       {
  1590.         /* To solve this, we need to know whether or not the symbol */
  1591.         /* appearing on the call instruction is a glue function or not. */
  1592.         /* A glue function must announce itself via a IMGLUE reloc, and */
  1593.         /* the reloc contains the required toc restore instruction */
  1594.       
  1595.         bfd_vma x;
  1596.         const char *my_name;
  1597.         DUMP_RELOC2(howto->name, rel);
  1598.  
  1599.         if (h != 0)
  1600.           {
  1601.         my_name = h->root.root.root.string;
  1602.         if (h->symbol_is_glue == 1) 
  1603.           {
  1604.             x = bfd_get_32(input_bfd, loc);
  1605.             bfd_put_32(input_bfd, h->glue_insn, loc);
  1606.           }
  1607.           }
  1608.       }
  1609.       break;
  1610.     case IMAGE_REL_PPC_SECREL:
  1611.       /* Unimplemented: codeview debugging information */
  1612.       /* For fast access to the header of the section 
  1613.          containing the item. */
  1614.       break;
  1615.     case IMAGE_REL_PPC_SECTION:
  1616.       /* Unimplemented: codeview debugging information */
  1617.       /* Is used to indicate that the value should be relative
  1618.          to the beginning of the section that contains the
  1619.          symbol */
  1620.       break;
  1621.     case IMAGE_REL_PPC_ABSOLUTE:
  1622.       {
  1623.         const char *my_name;
  1624.         if (h == 0)
  1625.         my_name = (syms+symndx)->_n._n_name;
  1626.         else
  1627.           {
  1628.         my_name = h->root.root.root.string;
  1629.           }
  1630.  
  1631.         fprintf(stderr, 
  1632.             "Warning: unsupported reloc %s <file %s, section %s>\n", 
  1633.             howto->name,
  1634.             bfd_get_filename(input_bfd),
  1635.             input_section->name);
  1636.  
  1637.         fprintf(stderr,"sym %ld (%s), r_vaddr %ld (%lx)\n", 
  1638.             rel->r_symndx, my_name, (long) rel->r_vaddr,
  1639.             (unsigned long) rel->r_vaddr);  
  1640.       }
  1641.       break;
  1642.     case IMAGE_REL_PPC_IMGLUE:
  1643.       {
  1644.         /* There is nothing to do now. This reloc was noted in the first
  1645.            pass over the relocs, and the glue instruction extracted */
  1646.         const char *my_name;
  1647.         if (h->symbol_is_glue == 1) 
  1648.           break;
  1649.         my_name = h->root.root.root.string;
  1650.         fprintf(stderr, 
  1651.             "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n", 
  1652.             howto->name,
  1653.             bfd_get_filename(input_bfd),
  1654.             input_section->name);
  1655.         break;
  1656.  
  1657.       }
  1658.       break;
  1659.  
  1660.     case IMAGE_REL_PPC_ADDR32NB:
  1661.       {
  1662.         struct coff_link_hash_entry *myh = 0;
  1663.         const char *name = 0;
  1664.         DUMP_RELOC2(howto->name, rel);
  1665.  
  1666.         if (strncmp(".idata$2",input_section->name,8) == 0 && first_thunk_address == 0)
  1667.           {
  1668.         /* set magic values */
  1669.         int idata5offset;
  1670.         struct coff_link_hash_entry *myh = 0;
  1671.         myh = coff_link_hash_lookup (coff_hash_table (info),
  1672.                          "__idata5_magic__",
  1673.                          false, false, true);
  1674.         first_thunk_address = myh->root.u.def.value + 
  1675.           sec->output_section->vma + 
  1676.             sec->output_offset - 
  1677.               pe_data(output_bfd)->pe_opthdr.ImageBase;
  1678.         
  1679.         idata5offset = myh->root.u.def.value;
  1680.         myh = coff_link_hash_lookup (coff_hash_table (info),
  1681.                          "__idata6_magic__",
  1682.                          false, false, true);
  1683.         
  1684.         thunk_size = myh->root.u.def.value - idata5offset;
  1685.         myh = coff_link_hash_lookup (coff_hash_table (info),
  1686.                          "__idata4_magic__",
  1687.                          false, false, true);
  1688.         import_table_size = myh->root.u.def.value;
  1689. #ifdef DEBUG_RELOC
  1690.         fprintf(stderr,
  1691.             "first computation triggered fta %x, ts %d(%x), its %d(%x)\n",
  1692.             first_thunk_address, thunk_size, thunk_size, import_table_size,
  1693.             import_table_size);
  1694. #endif
  1695.           }
  1696.  
  1697.         if (h == 0)
  1698.           { /* it is a file local symbol */
  1699.         sym = syms + symndx;
  1700.         name = sym->_n._n_name;
  1701.           }
  1702.         else
  1703.           {
  1704.         char *target = 0;
  1705.  
  1706.         name = h->root.root.root.string;
  1707.         if (strcmp(".idata$2", name) == 0)
  1708.           target = "__idata2_magic__";
  1709.         else if (strcmp(".idata$4", name) == 0)
  1710.           target = "__idata4_magic__";
  1711.         else if (strcmp(".idata$5", name) == 0)
  1712.           target = "__idata5_magic__";
  1713.  
  1714.         if (target != 0)
  1715.           {
  1716.             myh = 0;
  1717.  
  1718.             myh = coff_link_hash_lookup (coff_hash_table (info),
  1719.                          target,
  1720.                          false, false, true);
  1721.             if (myh == 0) 
  1722.               {
  1723.             fprintf(stderr, "Missing idata magic cookies, this cannot work anyway...\n");
  1724.             abort();
  1725.               }
  1726.             
  1727.             val = myh->root.u.def.value + 
  1728.               sec->output_section->vma + sec->output_offset;
  1729.             if (first_thunk_address == 0)
  1730.               {
  1731.             int idata5offset;
  1732.             myh = coff_link_hash_lookup (coff_hash_table (info),
  1733.                              "__idata5_magic__",
  1734.                              false, false, true);
  1735.             first_thunk_address = myh->root.u.def.value + 
  1736.               sec->output_section->vma + 
  1737.                 sec->output_offset - 
  1738.                   pe_data(output_bfd)->pe_opthdr.ImageBase;
  1739.             
  1740.             idata5offset = myh->root.u.def.value;
  1741.             myh = coff_link_hash_lookup (coff_hash_table (info),
  1742.                              "__idata6_magic__",
  1743.                              false, false, true);
  1744.             
  1745.             thunk_size = myh->root.u.def.value - idata5offset;
  1746.             myh = coff_link_hash_lookup (coff_hash_table (info),
  1747.                              "__idata4_magic__",
  1748.                              false, false, true);
  1749.             import_table_size = myh->root.u.def.value;
  1750. #ifdef DEBUG_RELOC
  1751.  
  1752.             fprintf(stderr,
  1753.                 "second computation triggered fta %x, ts %d(%x), its %d(%x)\n",
  1754.                 first_thunk_address, thunk_size, thunk_size, import_table_size,
  1755.                 import_table_size);
  1756. #endif
  1757.               }
  1758.           }
  1759.           }
  1760.  
  1761.         rstat = _bfd_relocate_contents (howto,
  1762.                         input_bfd, 
  1763.                   val - 
  1764.                   pe_data(output_bfd)->pe_opthdr.ImageBase,
  1765.                   loc);
  1766.       }
  1767.       break;
  1768.  
  1769.     case IMAGE_REL_PPC_REL24:
  1770.       DUMP_RELOC2(howto->name, rel);
  1771.       val -= (input_section->output_section->vma
  1772.           + input_section->output_offset);
  1773.  
  1774.       rstat = _bfd_relocate_contents (howto,
  1775.                       input_bfd, 
  1776.                       val, 
  1777.                       loc);
  1778.       break;
  1779.     case IMAGE_REL_PPC_ADDR16:
  1780.     case IMAGE_REL_PPC_ADDR24:
  1781.     case IMAGE_REL_PPC_ADDR32:
  1782.       DUMP_RELOC2(howto->name, rel);
  1783.       rstat = _bfd_relocate_contents (howto,
  1784.                       input_bfd, 
  1785.                       val, 
  1786.                       loc);
  1787.       break;
  1788.     }
  1789.  
  1790.       if ( info->base_file )
  1791.     {
  1792.       /* So if this is non pcrelative, and is referenced
  1793.          to a section or a common symbol, then it needs a reloc */
  1794.       if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
  1795.         {
  1796.           /* relocation to a symbol in a section which
  1797.          isn't absolute - we output the address here 
  1798.          to a file */
  1799.           bfd_vma addr = rel->r_vaddr 
  1800.         - input_section->vma 
  1801.         + input_section->output_offset 
  1802.           + input_section->output_section->vma;
  1803.  
  1804.           if (coff_data(output_bfd)->pe)
  1805.         {
  1806. #ifdef DEBUG_RELOC
  1807.           bfd_vma before_addr = addr;
  1808. #endif
  1809.           addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
  1810. #ifdef DEBUG_RELOC
  1811.           fprintf(stderr,
  1812.               " adjusted down from %x to %x", before_addr, addr);
  1813. #endif
  1814.         }
  1815. #ifdef DEBUG_RELOC
  1816.           fprintf(stderr, "\n");
  1817. #endif
  1818.  
  1819.           fwrite (&addr, 1,4, (FILE *) info->base_file);
  1820.         }
  1821.     }
  1822.  
  1823.       switch (rstat)
  1824.     {
  1825.     default:
  1826.       abort ();
  1827.     case bfd_reloc_ok:
  1828.       break;
  1829.     case bfd_reloc_overflow:
  1830.       {
  1831.         const char *name;
  1832.         char buf[SYMNMLEN + 1];
  1833.  
  1834.         if (symndx == -1)
  1835.           name = "*ABS*";
  1836.         else if (h != NULL)
  1837.           name = h->root.root.root.string;
  1838.         else if (sym == NULL)
  1839.           name = "*unknown*";
  1840.         else if (sym->_n._n_n._n_zeroes == 0
  1841.              && sym->_n._n_n._n_offset != 0)
  1842.           name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
  1843.         else
  1844.           {
  1845.         strncpy (buf, sym->_n._n_name, SYMNMLEN);
  1846.         buf[SYMNMLEN] = '\0';
  1847.         name = buf;
  1848.           }
  1849. #if 0
  1850.         else
  1851.           {
  1852.         name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
  1853.         if (name == NULL)
  1854.           return false;
  1855.           }
  1856. #endif
  1857.  
  1858.         if (! ((*info->callbacks->reloc_overflow)
  1859.            (info, name, howto->name, 
  1860.             (bfd_vma) 0, input_bfd,
  1861.             input_section, rel->r_vaddr - input_section->vma)))
  1862.           {
  1863. #ifdef DEBUG_RELOC
  1864.         fprintf(stderr, 
  1865.             "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n", 
  1866.             TARGET_LITTLE_NAME,
  1867.             input_section->name,
  1868.             input_bfd->filename);
  1869.   
  1870. #endif  
  1871.         return false;
  1872.           }
  1873.       }
  1874.     }
  1875.  
  1876.     }     
  1877.  
  1878. #ifdef DEBUG_RELOC
  1879.   fprintf(stderr, 
  1880.       "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n", 
  1881.       TARGET_LITTLE_NAME,
  1882.       input_section->name,
  1883.       input_bfd->filename);
  1884.   
  1885. #endif  
  1886.  
  1887.   return true;
  1888. }
  1889.  
  1890.  
  1891. #ifdef COFF_IMAGE_WITH_PE
  1892.  
  1893. long int global_toc_size = 4;
  1894.  
  1895. bfd* bfd_of_toc_owner = 0;
  1896.  
  1897. long int import_table_size;
  1898. long int first_thunk_address;
  1899. long int thunk_size;
  1900.  
  1901. struct list_ele *head;
  1902. struct list_ele *tail;
  1903.  
  1904. static char *
  1905. h1 = "\n\t\t\tTOC MAPPING\n\n";
  1906. static char *
  1907. h2 = " TOC    disassembly  Comments       Name\n";
  1908. static char *
  1909. h3 = " Offset  spelling                   (if present)\n";
  1910.  
  1911. void
  1912. dump_toc(vfile)
  1913.      void *vfile;
  1914. {
  1915.   FILE *file = vfile;
  1916.   struct list_ele *t;
  1917.  
  1918.   fprintf(file, h1);
  1919.   fprintf(file, h2);
  1920.   fprintf(file, h3);
  1921.  
  1922.   for(t = head; t != 0; t=t->next)
  1923.     {
  1924.       char *cat;
  1925.  
  1926.       if (t->cat == priv)
  1927.     cat = "private       ";
  1928.       else if (t->cat == pub)
  1929.     cat = "public        ";
  1930.       else if (t->cat == data)
  1931.     cat = "data-in-toc   ";
  1932.  
  1933.       if (t->offset > global_toc_size)
  1934.     {
  1935.       if (t->offset <= global_toc_size + thunk_size)
  1936.         cat = "IAT reference ";
  1937.       else
  1938.         {
  1939.           fprintf(file,
  1940.               "**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n",
  1941.               global_toc_size, global_toc_size, thunk_size, thunk_size);
  1942.           cat = "Out of bounds!";
  1943.         }
  1944.     }
  1945.  
  1946.       fprintf(file,
  1947.           " %04lx    (%d)", (unsigned long) t->offset, t->offset - 32768);
  1948.       fprintf(file,
  1949.           "    %s %s\n",
  1950.           cat, t->name);
  1951.  
  1952.     }
  1953.  
  1954.   fprintf(file, "\n");
  1955. }
  1956.  
  1957. boolean
  1958. ppc_allocate_toc_section (info) 
  1959.      struct bfd_link_info *info;
  1960. {
  1961.   asection *s;
  1962.   bfd_byte *foo;
  1963.   static char test_char = '1';
  1964.  
  1965.   if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
  1966.     return true;
  1967.  
  1968.   if (bfd_of_toc_owner == 0)
  1969.     {
  1970.       fprintf(stderr,
  1971.           "There is no bfd that owns the toc section!\n");
  1972.       abort();
  1973.     }
  1974.  
  1975.   s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
  1976.   if (s == NULL) 
  1977.     {
  1978.       fprintf(stderr, "No Toc section!\n");
  1979.       abort();
  1980.     }
  1981.  
  1982.   foo = (bfd_byte *) bfd_alloc(bfd_of_toc_owner, global_toc_size);
  1983.   memset(foo, test_char, global_toc_size);
  1984.  
  1985.   s->_raw_size = s->_cooked_size = global_toc_size;
  1986.   s->contents = foo;
  1987.  
  1988.   return true;
  1989. }
  1990.  
  1991. boolean
  1992. ppc_process_before_allocation (abfd, info)
  1993.      bfd *abfd;
  1994.      struct bfd_link_info *info;
  1995. {
  1996.   asection *sec;
  1997.   struct internal_reloc *i, *rel;
  1998.  
  1999. #ifdef DEBUG_RELOC
  2000.   fprintf(stderr, 
  2001.       "ppc_process_before_allocation: BFD %s\n", 
  2002.       bfd_get_filename(abfd));
  2003. #endif
  2004.  
  2005.   /* here we have a bfd that is to be included on the link. We have a hook
  2006.      to do reloc rummaging, before section sizes are nailed down. */
  2007.  
  2008.   _bfd_coff_get_external_symbols(abfd);
  2009.  
  2010.   /* rummage around all the relocs and map the toc */
  2011.   sec = abfd->sections;
  2012.  
  2013.   if (sec == 0)
  2014.     {
  2015.       return true;
  2016.     }
  2017.  
  2018.   for (; sec != 0; sec = sec->next)
  2019.   {
  2020.     int toc_offset;
  2021.  
  2022. #ifdef DEBUG_RELOC
  2023.     fprintf(stderr, 
  2024.         "  section %s reloc count %d\n", 
  2025.         sec->name, 
  2026.         sec->reloc_count);
  2027. #endif
  2028.  
  2029.     if (sec->reloc_count == 0) 
  2030.       continue;
  2031.  
  2032.     /* load the relocs */
  2033.     /* FIXME: there may be a storage leak here */
  2034.     i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
  2035.     
  2036.     if (i == 0)
  2037.       abort();
  2038.  
  2039.     for (rel=i;rel<i+sec->reloc_count;++rel) 
  2040.       {
  2041.     unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
  2042.     unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
  2043.  
  2044. #ifdef DEBUG_RELOC
  2045.     /* now examine flags */
  2046.     if (r_flags != 0) 
  2047.       {
  2048.         fprintf (stderr, "Reloc with flags found!");
  2049.         if ( r_flags & IMAGE_REL_PPC_NEG ) 
  2050.           fprintf (stderr, " NEG");
  2051.         if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
  2052.           fprintf (stderr, " BRTAKEN");
  2053.         if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
  2054.           fprintf (stderr, " BRNTAKEN");
  2055.         if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
  2056.         fprintf (stderr, " TOCDEFN");
  2057.         fprintf(stderr, "\n");
  2058.       }
  2059. #endif
  2060.     
  2061.     DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
  2062.  
  2063.     switch(r_type) 
  2064.       {
  2065.       case IMAGE_REL_PPC_TOCREL16:
  2066. #if 0
  2067.         /* FIXME:
  2068.            This remains unimplemented for now, as it currently adds
  2069.            un-necessary elements to the toc. All we need to do today
  2070.            is not do anything if TOCDEFN is on.
  2071.         */
  2072.         if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
  2073.           toc_offset = ppc_record_data_in_toc_entry(abfd, info, sec, 
  2074.                             rel->r_symndx, 
  2075.                             default_toc);
  2076.         else
  2077.           toc_offset = ppc_record_toc_entry(abfd, info, sec, 
  2078.                         rel->r_symndx, default_toc);
  2079. #endif
  2080.         if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN )
  2081.           toc_offset = ppc_record_toc_entry(abfd, info, sec, 
  2082.                         rel->r_symndx, default_toc);
  2083.         break;
  2084.       case IMAGE_REL_PPC_IMGLUE:
  2085.         ppc_mark_symbol_as_glue(abfd, rel->r_symndx, rel);
  2086.         break;
  2087.       default:
  2088.         break;
  2089.       }
  2090.       }
  2091.   }
  2092.  
  2093.   return true;
  2094. }
  2095.  
  2096. #endif
  2097.  
  2098.  
  2099. static bfd_reloc_status_type
  2100. ppc_refhi_reloc (abfd,
  2101.          reloc_entry,
  2102.          symbol,
  2103.          data,
  2104.          input_section,
  2105.          output_bfd,
  2106.          error_message)
  2107.      bfd *abfd;
  2108.      arelent *reloc_entry;
  2109.      asymbol *symbol;
  2110.      PTR data;
  2111.      asection *input_section;
  2112.      bfd *output_bfd;
  2113.      char **error_message;
  2114. {
  2115.   UN_IMPL("REFHI");
  2116.   DUMP_RELOC("REFHI",reloc_entry);
  2117.  
  2118.   if (output_bfd == (bfd *) NULL)
  2119.     return bfd_reloc_continue;
  2120.  
  2121.   return bfd_reloc_undefined;
  2122. }
  2123.  
  2124. #if 0
  2125.  
  2126. static bfd_reloc_status_type
  2127. ppc_reflo_reloc (abfd,
  2128.          reloc_entry,
  2129.          symbol,
  2130.          data,
  2131.          input_section,
  2132.          output_bfd,
  2133.          error_message)
  2134.      bfd *abfd;
  2135.      arelent *reloc_entry;
  2136.      asymbol *symbol;
  2137.      PTR data;
  2138.      asection *input_section;
  2139.      bfd *output_bfd;
  2140.      char **error_message;
  2141. {
  2142.   UN_IMPL("REFLO");
  2143.   DUMP_RELOC("REFLO",reloc_entry);
  2144.  
  2145.   if (output_bfd == (bfd *) NULL)
  2146.     return bfd_reloc_continue;
  2147.  
  2148.   return bfd_reloc_undefined;
  2149. }
  2150.  
  2151. #endif
  2152.  
  2153. static bfd_reloc_status_type
  2154. ppc_pair_reloc (abfd,
  2155.         reloc_entry,
  2156.         symbol,
  2157.         data,
  2158.         input_section,
  2159.         output_bfd,
  2160.         error_message)
  2161.      bfd *abfd;
  2162.      arelent *reloc_entry;
  2163.      asymbol *symbol;
  2164.      PTR data;
  2165.      asection *input_section;
  2166.      bfd *output_bfd;
  2167.      char **error_message;
  2168. {
  2169.   UN_IMPL("PAIR");
  2170.   DUMP_RELOC("PAIR",reloc_entry);
  2171.  
  2172.   if (output_bfd == (bfd *) NULL)
  2173.     return bfd_reloc_continue;
  2174.  
  2175.   return bfd_reloc_undefined;
  2176. }
  2177.  
  2178.  
  2179. static bfd_reloc_status_type
  2180. ppc_toc16_reloc (abfd,
  2181.          reloc_entry,
  2182.          symbol,
  2183.          data,
  2184.          input_section,
  2185.          output_bfd,
  2186.          error_message)
  2187.      bfd *abfd;
  2188.      arelent *reloc_entry;
  2189.      asymbol *symbol;
  2190.      PTR data;
  2191.      asection *input_section;
  2192.      bfd *output_bfd;
  2193.      char **error_message;
  2194. {
  2195.   UN_IMPL("TOCREL16");
  2196.   DUMP_RELOC("TOCREL16",reloc_entry);
  2197.  
  2198.   if (output_bfd == (bfd *) NULL)
  2199.     {
  2200.       return bfd_reloc_continue;
  2201.     }
  2202.  
  2203.   return bfd_reloc_ok;
  2204. }
  2205.  
  2206. #if 0
  2207.  
  2208. /* ADDR32NB : 32 bit address relative to the virtual origin.         */
  2209. /*            (On the alpha, this is always a linker generated thunk)*/
  2210. /*            (i.e. 32bit addr relative to the image base)           */
  2211. /*                                                                   */
  2212. /*                                                                   */
  2213.  
  2214. static bfd_reloc_status_type
  2215. ppc_addr32nb_reloc (abfd,
  2216.             reloc_entry,
  2217.             symbol,
  2218.             data,
  2219.             input_section,
  2220.             output_bfd,
  2221.             error_message)
  2222.      bfd *abfd;
  2223.      arelent *reloc_entry;
  2224.      asymbol *symbol;
  2225.      PTR data;
  2226.      asection *input_section;
  2227.      bfd *output_bfd;
  2228.      char **error_message;
  2229. {
  2230.   UN_IMPL("ADDR32NB");
  2231.   DUMP_RELOC("ADDR32NB",reloc_entry);
  2232.  
  2233.   return bfd_reloc_ok;
  2234. }
  2235.  
  2236. #endif
  2237.  
  2238. static bfd_reloc_status_type
  2239. ppc_secrel_reloc (abfd,
  2240.           reloc_entry,
  2241.           symbol,
  2242.           data,
  2243.           input_section,
  2244.           output_bfd,
  2245.           error_message)
  2246.      bfd *abfd;
  2247.      arelent *reloc_entry;
  2248.      asymbol *symbol;
  2249.      PTR data;
  2250.      asection *input_section;
  2251.      bfd *output_bfd;
  2252.      char **error_message;
  2253. {
  2254.   UN_IMPL("SECREL");
  2255.   DUMP_RELOC("SECREL",reloc_entry);
  2256.  
  2257.   if (output_bfd == (bfd *) NULL)
  2258.     return bfd_reloc_continue;
  2259.  
  2260.   return bfd_reloc_ok;
  2261. }
  2262.  
  2263. static bfd_reloc_status_type
  2264. ppc_section_reloc (abfd,
  2265.            reloc_entry,
  2266.            symbol,
  2267.            data,
  2268.            input_section,
  2269.            output_bfd,
  2270.            error_message)
  2271.      bfd *abfd;
  2272.      arelent *reloc_entry;
  2273.      asymbol *symbol;
  2274.      PTR data;
  2275.      asection *input_section;
  2276.      bfd *output_bfd;
  2277.      char **error_message;
  2278. {
  2279.   UN_IMPL("SECTION");
  2280.   DUMP_RELOC("SECTION",reloc_entry);
  2281.  
  2282.   if (output_bfd == (bfd *) NULL)
  2283.     return bfd_reloc_continue;
  2284.  
  2285.   return bfd_reloc_ok;
  2286. }
  2287.  
  2288. static bfd_reloc_status_type
  2289. ppc_imglue_reloc (abfd,
  2290.           reloc_entry,
  2291.           symbol,
  2292.           data,
  2293.           input_section,
  2294.           output_bfd,
  2295.           error_message)
  2296.      bfd *abfd;
  2297.      arelent *reloc_entry;
  2298.      asymbol *symbol;
  2299.      PTR data;
  2300.      asection *input_section;
  2301.      bfd *output_bfd;
  2302.      char **error_message;
  2303. {
  2304.   UN_IMPL("IMGLUE");
  2305.   DUMP_RELOC("IMGLUE",reloc_entry);
  2306.  
  2307.   if (output_bfd == (bfd *) NULL)
  2308.     return bfd_reloc_continue;
  2309.  
  2310.   return bfd_reloc_ok;
  2311. }
  2312.  
  2313.  
  2314.  
  2315. #define MAX_RELOC_INDEX  \
  2316.       (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
  2317.  
  2318.  
  2319. /* FIXME: There is a possiblity that when we read in a reloc from a file,
  2320.           that there are some bits encoded in the upper portion of the 
  2321.       type field. Not yet implemented.
  2322. */
  2323. static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
  2324.                       struct internal_reloc *internal));
  2325.  
  2326. static void
  2327. ppc_coff_rtype2howto (relent, internal)
  2328.      arelent *relent;
  2329.      struct internal_reloc *internal;
  2330. {  
  2331.  
  2332.   /* We can encode one of three things in the type field, aside from the
  2333.      type:
  2334.      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
  2335.         value, rather than an addition value
  2336.      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
  2337.         the branch is expected to be taken or not.
  2338.      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
  2339.      For now, we just strip this stuff to find the type, and ignore it other
  2340.      than that.
  2341.   */
  2342.   reloc_howto_type *howto;
  2343.   unsigned short r_type  = EXTRACT_TYPE (internal->r_type);
  2344.   unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
  2345.   unsigned short junk    = EXTRACT_JUNK (internal->r_type);
  2346.  
  2347.   /* the masking process only slices off the bottom byte for r_type. */
  2348.   if ( r_type > MAX_RELOC_INDEX ) 
  2349.     {
  2350.       fprintf(stderr, 
  2351.           "ppc_coff_rtype2howto: reloc index %d out of range [%d, %ld]\n",
  2352.           internal->r_type, 0, (long) MAX_RELOC_INDEX);
  2353.       abort();
  2354.     }
  2355.  
  2356.   /* check for absolute crap */
  2357.   if ( junk != 0 )
  2358.     {
  2359.       fprintf(stderr, 
  2360.           "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
  2361.           internal->r_type, junk);
  2362.       abort();
  2363.     }
  2364.  
  2365. #ifdef DEBUG_RELOC
  2366.   /* now examine flags */
  2367.   if (r_flags != 0) 
  2368.     {
  2369.       fprintf (stderr, "Reloc with flags found!");
  2370.       if ( r_flags & IMAGE_REL_PPC_NEG ) 
  2371.     fprintf (stderr, " NEG");
  2372.       if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
  2373.     fprintf (stderr, " BRTAKEN");
  2374.       if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
  2375.     fprintf (stderr, " BRNTAKEN");
  2376.       if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
  2377.     fprintf (stderr, " TOCDEFN");
  2378.       fprintf(stderr, "\n");
  2379.     }
  2380. #endif
  2381.  
  2382.   switch(r_type) 
  2383.     {
  2384.     case IMAGE_REL_PPC_ADDR16:
  2385.     case IMAGE_REL_PPC_REL24:
  2386.     case IMAGE_REL_PPC_ADDR24:
  2387.     case IMAGE_REL_PPC_ADDR32:
  2388.     case IMAGE_REL_PPC_IFGLUE:
  2389.     case IMAGE_REL_PPC_ADDR32NB:
  2390.     case IMAGE_REL_PPC_SECTION:
  2391.     case IMAGE_REL_PPC_SECREL:
  2392.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
  2393.       howto = ppc_coff_howto_table + r_type;
  2394.       break;
  2395.     case IMAGE_REL_PPC_IMGLUE:
  2396.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
  2397.       howto = ppc_coff_howto_table + r_type;
  2398.       break;
  2399.     case IMAGE_REL_PPC_TOCREL16:
  2400.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
  2401.       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
  2402.     howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
  2403.       else
  2404.     howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
  2405.       break;
  2406.     default:
  2407.       fprintf(stderr, 
  2408.           "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
  2409.           ppc_coff_howto_table[r_type].name,
  2410.           r_type);
  2411.       howto = ppc_coff_howto_table + r_type;      
  2412.       break;
  2413.     }
  2414.   
  2415.   relent->howto = howto;
  2416.   
  2417. }
  2418.  
  2419. static reloc_howto_type *
  2420. coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
  2421.      bfd *abfd;
  2422.      asection *sec;
  2423.      struct internal_reloc *rel;
  2424.      struct coff_link_hash_entry *h;
  2425.      struct internal_syment *sym;
  2426.      bfd_vma *addendp;
  2427. {
  2428.   reloc_howto_type *howto;
  2429.  
  2430.   /* We can encode one of three things in the type field, aside from the
  2431.      type:
  2432.      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
  2433.         value, rather than an addition value
  2434.      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
  2435.         the branch is expected to be taken or not.
  2436.      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
  2437.      For now, we just strip this stuff to find the type, and ignore it other
  2438.      than that.
  2439.   */
  2440.  
  2441.   unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
  2442.   unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
  2443.   unsigned short junk    = EXTRACT_JUNK (rel->r_type);
  2444.  
  2445.   /* the masking process only slices off the bottom byte for r_type. */
  2446.   if ( r_type > MAX_RELOC_INDEX ) 
  2447.     {
  2448.       fprintf(stderr, 
  2449.           "coff_ppc_rtype_to_howto: index %d out of range [%d, %ld]\n",
  2450.           r_type, 0, (long) MAX_RELOC_INDEX);
  2451.       abort();
  2452.     }
  2453.   
  2454.   /* check for absolute crap */
  2455.   if ( junk != 0 )
  2456.     {
  2457.       fprintf(stderr, 
  2458.           "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
  2459.           rel->r_type, junk);
  2460.       abort();
  2461.     }
  2462.   
  2463. #ifdef DEBUG_RELOC
  2464.   /* now examine flags */
  2465.   if (r_flags != 0) 
  2466.     {
  2467.       fprintf (stderr, "Reloc with flags found!");
  2468.       if ( r_flags & IMAGE_REL_PPC_NEG ) 
  2469.     fprintf (stderr, " NEG");
  2470.       if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
  2471.     fprintf (stderr, " BRTAKEN");
  2472.       if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
  2473.     fprintf (stderr, " BRNTAKEN");
  2474.       if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
  2475.     fprintf (stderr, " TOCDEFN");
  2476.       fprintf(stderr, "\n");
  2477.     }
  2478. #endif
  2479.   
  2480.   switch(r_type) 
  2481.     {
  2482.     case IMAGE_REL_PPC_ADDR32NB:
  2483.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
  2484.       *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
  2485.       howto = ppc_coff_howto_table + r_type;
  2486.       break;
  2487.     case IMAGE_REL_PPC_TOCREL16:
  2488.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
  2489.       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
  2490.     howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
  2491.       else
  2492.     howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
  2493.       break;
  2494.     case IMAGE_REL_PPC_ADDR16:
  2495.     case IMAGE_REL_PPC_REL24:
  2496.     case IMAGE_REL_PPC_ADDR24:
  2497.     case IMAGE_REL_PPC_ADDR32:
  2498.     case IMAGE_REL_PPC_IFGLUE:
  2499.     case IMAGE_REL_PPC_SECTION:
  2500.     case IMAGE_REL_PPC_SECREL:
  2501.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
  2502.       howto = ppc_coff_howto_table + r_type;
  2503.       break;
  2504.     case IMAGE_REL_PPC_IMGLUE:
  2505.       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
  2506.       howto = ppc_coff_howto_table + r_type;
  2507.       break;
  2508.     default:
  2509.       fprintf(stderr, 
  2510.           "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
  2511.           ppc_coff_howto_table[r_type].name,
  2512.           r_type);
  2513.       howto = ppc_coff_howto_table + r_type;
  2514.       break;
  2515.     }
  2516.   
  2517.   return howto;
  2518. }
  2519.  
  2520.  
  2521. /* a cheesy little macro to make the code a little more readable */
  2522. #define HOW2MAP(bfd_rtype,ppc_rtype)  \
  2523.  case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
  2524.  
  2525. static reloc_howto_type *ppc_coff_reloc_type_lookup
  2526. PARAMS ((bfd *, bfd_reloc_code_real_type));
  2527.  
  2528. static reloc_howto_type *
  2529. ppc_coff_reloc_type_lookup (abfd, code)
  2530.      bfd *abfd;
  2531.      bfd_reloc_code_real_type code;
  2532. {
  2533.   
  2534. #ifdef DEBUG_RELOC
  2535.   fprintf(stderr, "ppc_coff_reloc_type_lookup for %s\n",
  2536.       bfd_get_reloc_code_name(code));
  2537. #endif
  2538.  
  2539.   switch (code)
  2540.     {
  2541.       HOW2MAP(BFD_RELOC_32_GOTOFF,    IMAGE_REL_PPC_IMGLUE);
  2542.       HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
  2543.       HOW2MAP(BFD_RELOC_16,           IMAGE_REL_PPC_ADDR16);
  2544.       HOW2MAP(BFD_RELOC_PPC_B26,      IMAGE_REL_PPC_REL24);
  2545.       HOW2MAP(BFD_RELOC_PPC_BA26,     IMAGE_REL_PPC_ADDR24);
  2546.       HOW2MAP(BFD_RELOC_PPC_TOC16,    IMAGE_REL_PPC_TOCREL16);
  2547.       HOW2MAP(BFD_RELOC_16_GOTOFF,    IMAGE_REL_PPC_TOCREL16_DEFN);
  2548.       HOW2MAP(BFD_RELOC_32,           IMAGE_REL_PPC_ADDR32);
  2549.       HOW2MAP(BFD_RELOC_RVA,          IMAGE_REL_PPC_ADDR32NB);
  2550.     default: 
  2551.       return NULL;
  2552.     }
  2553.   /*NOTREACHED*/
  2554. }
  2555.  
  2556. #undef HOW2MAP
  2557.  
  2558.  
  2559. /* Tailor coffcode.h -- macro heaven. */
  2560.  
  2561. #define RTYPE2HOWTO(cache_ptr, dst)  ppc_coff_rtype2howto (cache_ptr, dst)
  2562.  
  2563. #ifndef COFF_IMAGE_WITH_PE
  2564. static void
  2565. ppc_coff_swap_sym_in_hook ();
  2566. #endif
  2567.  
  2568. /* We use the special COFF backend linker, with our own special touch.  */
  2569.  
  2570. #define coff_bfd_reloc_type_lookup   ppc_coff_reloc_type_lookup
  2571. #define coff_rtype_to_howto          coff_ppc_rtype_to_howto
  2572. #define coff_relocate_section        coff_ppc_relocate_section
  2573. #define coff_bfd_final_link          ppc_bfd_coff_final_link 
  2574.  
  2575. #ifndef COFF_IMAGE_WITH_PE
  2576. #define coff_swap_sym_in_hook        ppc_coff_swap_sym_in_hook
  2577. #endif
  2578.  
  2579. #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
  2580.  
  2581. #define COFF_PAGE_SIZE                       0x1000
  2582.  
  2583. #define POWERPC_LE_PE
  2584.  
  2585. #include "coffcode.h"
  2586.  
  2587.  
  2588.  
  2589. #ifndef COFF_IMAGE_WITH_PE
  2590. /* FIXME:
  2591.    What we're trying to do here is allocate a toc section (early), and attach 
  2592.    it to the last bfd to be processed. This avoids the problem of having a toc
  2593.    written out before all files have been processed. This code allocates
  2594.    a toc section for every file, and records the last one seen. There are
  2595.    at least two problems with this approach:
  2596.    1. We allocate whole bunches of toc sections that are ignored, but at
  2597.       at least we will not allocate a toc if no .toc is present.
  2598.    2. It's not clear to me that being the last bfd read necessarily means
  2599.       that you are the last bfd closed.
  2600.    3. Doing it on a "swap in" hook depends on when the "swap in" is called,
  2601.       and how often, etc. It's not clear to me that there isn't a hole here.
  2602. */
  2603.  
  2604. static void
  2605. ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
  2606.      bfd            *abfd;
  2607.      PTR ext1;
  2608.      PTR in1;
  2609. {
  2610.   struct internal_syment      *in = (struct internal_syment *)in1;
  2611.  
  2612.   if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
  2613.     return;
  2614.  
  2615.   if (strcmp(in->_n._n_name, ".toc") == 0)
  2616.     {
  2617.       flagword flags;
  2618.       register asection *s;
  2619.  
  2620.       s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
  2621.       if (s != NULL) 
  2622.     {
  2623.       return;
  2624.     }
  2625.  
  2626.       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
  2627.  
  2628. #ifdef TOC_DEBUG
  2629.       fprintf(stderr,
  2630.           "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
  2631.           TOC_SECTION_NAME);
  2632. #endif
  2633.  
  2634.       s = bfd_make_section (abfd, TOC_SECTION_NAME);
  2635.  
  2636.       if (s == NULL
  2637.       || !bfd_set_section_flags (abfd, s, flags)
  2638.       || !bfd_set_section_alignment (abfd, s, 2))
  2639.     {
  2640.       fprintf(stderr,
  2641.           "toc section allocation failed!\n");
  2642.       abort();
  2643.     }
  2644.  
  2645.       /* save the bfd for later allocation */
  2646.       bfd_of_toc_owner = abfd;
  2647.     }
  2648.  
  2649.   return;
  2650. }
  2651. #endif
  2652.  
  2653. boolean
  2654. ppc_bfd_coff_final_link ();
  2655.  
  2656. #ifndef COFF_IMAGE_WITH_PE
  2657.  
  2658. static boolean
  2659. ppc_do_last(abfd)
  2660.      bfd *abfd;
  2661. {
  2662.   if (abfd == bfd_of_toc_owner)
  2663.     return true;
  2664.   else
  2665.     return false;
  2666. }
  2667.  
  2668. static bfd *
  2669. ppc_get_last()
  2670. {
  2671.   return bfd_of_toc_owner;
  2672. }
  2673.  
  2674. /* this piece of machinery exists only to guarantee that the bfd that holds
  2675.    the toc section is written last. 
  2676.  
  2677.    This does depend on bfd_make_section attaching a new section to the
  2678.    end of the section list for the bfd. 
  2679.  
  2680.    This is otherwise intended to be functionally the same as 
  2681.    cofflink.c:_bfd_coff_final_link(). It is specifically different only 
  2682.    where the POWERPC_LE_PE macro modifies the code. It is left in as a 
  2683.    precise form of comment. krk@cygnus.com
  2684. */
  2685. #define POWERPC_LE_PE
  2686.  
  2687.  
  2688. /* Do the final link step.  */
  2689.  
  2690. boolean
  2691. ppc_bfd_coff_final_link (abfd, info)
  2692.      bfd *abfd;
  2693.      struct bfd_link_info *info;
  2694. {
  2695.   bfd_size_type symesz;
  2696.   struct coff_final_link_info finfo;
  2697.   boolean debug_merge_allocated;
  2698.   asection *o;
  2699.   struct bfd_link_order *p;
  2700.   size_t max_sym_count;
  2701.   size_t max_lineno_count;
  2702.   size_t max_reloc_count;
  2703.   size_t max_output_reloc_count;
  2704.   size_t max_contents_size;
  2705.   file_ptr rel_filepos;
  2706.   unsigned int relsz;
  2707.   file_ptr line_filepos;
  2708.   unsigned int linesz;
  2709.   bfd *sub;
  2710.   bfd_byte *external_relocs = NULL;
  2711.   char strbuf[STRING_SIZE_SIZE];
  2712.  
  2713.   symesz = bfd_coff_symesz (abfd);
  2714.  
  2715.   finfo.info = info;
  2716.   finfo.output_bfd = abfd;
  2717.   finfo.strtab = NULL;
  2718.   finfo.section_info = NULL;
  2719.   finfo.last_file_index = -1;
  2720.   finfo.last_bf_index = -1;
  2721.   finfo.internal_syms = NULL;
  2722.   finfo.sec_ptrs = NULL;
  2723.   finfo.sym_indices = NULL;
  2724.   finfo.outsyms = NULL;
  2725.   finfo.linenos = NULL;
  2726.   finfo.contents = NULL;
  2727.   finfo.external_relocs = NULL;
  2728.   finfo.internal_relocs = NULL;
  2729.   debug_merge_allocated = false;
  2730.  
  2731.   coff_data (abfd)->link_info = info;
  2732.  
  2733.   finfo.strtab = _bfd_stringtab_init ();
  2734.   if (finfo.strtab == NULL)
  2735.     goto error_return;
  2736.  
  2737.   if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
  2738.     goto error_return;
  2739.   debug_merge_allocated = true;
  2740.  
  2741.   /* Compute the file positions for all the sections.  */
  2742.   if (! abfd->output_has_begun)
  2743.     bfd_coff_compute_section_file_positions (abfd);
  2744.  
  2745.   /* Count the line numbers and relocation entries required for the
  2746.      output file.  Set the file positions for the relocs.  */
  2747.   rel_filepos = obj_relocbase (abfd);
  2748.   relsz = bfd_coff_relsz (abfd);
  2749.   max_contents_size = 0;
  2750.   max_lineno_count = 0;
  2751.   max_reloc_count = 0;
  2752.  
  2753.   for (o = abfd->sections; o != NULL; o = o->next)
  2754.     {
  2755.       o->reloc_count = 0;
  2756.       o->lineno_count = 0;
  2757.       for (p = o->link_order_head; p != NULL; p = p->next)
  2758.     {
  2759.  
  2760.       if (p->type == bfd_indirect_link_order)
  2761.         {
  2762.           asection *sec;
  2763.  
  2764.           sec = p->u.indirect.section;
  2765.  
  2766.           /* Mark all sections which are to be included in the
  2767.          link.  This will normally be every section.  We need
  2768.          to do this so that we can identify any sections which
  2769.          the linker has decided to not include.  */
  2770.           sec->linker_mark = true;
  2771.  
  2772.           if (info->strip == strip_none
  2773.           || info->strip == strip_some)
  2774.         o->lineno_count += sec->lineno_count;
  2775.  
  2776.           if (info->relocateable)
  2777.         o->reloc_count += sec->reloc_count;
  2778.  
  2779.           if (sec->_raw_size > max_contents_size)
  2780.         max_contents_size = sec->_raw_size;
  2781.           if (sec->lineno_count > max_lineno_count)
  2782.         max_lineno_count = sec->lineno_count;
  2783.           if (sec->reloc_count > max_reloc_count)
  2784.         max_reloc_count = sec->reloc_count;
  2785.         }
  2786.       else if (info->relocateable
  2787.            && (p->type == bfd_section_reloc_link_order
  2788.                || p->type == bfd_symbol_reloc_link_order))
  2789.         ++o->reloc_count;
  2790.     }
  2791.       if (o->reloc_count == 0)
  2792.     o->rel_filepos = 0;
  2793.       else
  2794.     {
  2795.       o->flags |= SEC_RELOC;
  2796.       o->rel_filepos = rel_filepos;
  2797.       rel_filepos += o->reloc_count * relsz;
  2798.     }
  2799.     }
  2800.  
  2801.   /* If doing a relocateable link, allocate space for the pointers we
  2802.      need to keep.  */
  2803.   if (info->relocateable)
  2804.     {
  2805.       unsigned int i;
  2806.  
  2807.       /* We use section_count + 1, rather than section_count, because
  2808.          the target_index fields are 1 based.  */
  2809.       finfo.section_info =
  2810.     ((struct coff_link_section_info *)
  2811.      bfd_malloc ((abfd->section_count + 1)
  2812.              * sizeof (struct coff_link_section_info)));
  2813.       if (finfo.section_info == NULL)
  2814.     goto error_return;
  2815.       for (i = 0; i <= abfd->section_count; i++)
  2816.     {
  2817.       finfo.section_info[i].relocs = NULL;
  2818.       finfo.section_info[i].rel_hashes = NULL;
  2819.     }
  2820.     }
  2821.  
  2822.   /* We now know the size of the relocs, so we can determine the file
  2823.      positions of the line numbers.  */
  2824.   line_filepos = rel_filepos;
  2825.   linesz = bfd_coff_linesz (abfd);
  2826.   max_output_reloc_count = 0;
  2827.   for (o = abfd->sections; o != NULL; o = o->next)
  2828.     {
  2829.       if (o->lineno_count == 0)
  2830.     o->line_filepos = 0;
  2831.       else
  2832.     {
  2833.       o->line_filepos = line_filepos;
  2834.       line_filepos += o->lineno_count * linesz;
  2835.     }
  2836.  
  2837.       if (o->reloc_count != 0)
  2838.     {
  2839.       /* We don't know the indices of global symbols until we have
  2840.              written out all the local symbols.  For each section in
  2841.              the output file, we keep an array of pointers to hash
  2842.              table entries.  Each entry in the array corresponds to a
  2843.              reloc.  When we find a reloc against a global symbol, we
  2844.              set the corresponding entry in this array so that we can
  2845.              fix up the symbol index after we have written out all the
  2846.              local symbols.
  2847.  
  2848.          Because of this problem, we also keep the relocs in
  2849.          memory until the end of the link.  This wastes memory,
  2850.          but only when doing a relocateable link, which is not the
  2851.          common case.  */
  2852.       BFD_ASSERT (info->relocateable);
  2853.       finfo.section_info[o->target_index].relocs =
  2854.         ((struct internal_reloc *)
  2855.          bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
  2856.       finfo.section_info[o->target_index].rel_hashes =
  2857.         ((struct coff_link_hash_entry **)
  2858.          bfd_malloc (o->reloc_count
  2859.              * sizeof (struct coff_link_hash_entry *)));
  2860.       if (finfo.section_info[o->target_index].relocs == NULL
  2861.           || finfo.section_info[o->target_index].rel_hashes == NULL)
  2862.         goto error_return;
  2863.  
  2864.       if (o->reloc_count > max_output_reloc_count)
  2865.         max_output_reloc_count = o->reloc_count;
  2866.     }
  2867.  
  2868.       /* Reset the reloc and lineno counts, so that we can use them to
  2869.      count the number of entries we have output so far.  */
  2870.       o->reloc_count = 0;
  2871.       o->lineno_count = 0;
  2872.     }
  2873.  
  2874.   obj_sym_filepos (abfd) = line_filepos;
  2875.  
  2876.   /* Figure out the largest number of symbols in an input BFD.  Take
  2877.      the opportunity to clear the output_has_begun fields of all the
  2878.      input BFD's.  */
  2879.   max_sym_count = 0;
  2880.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  2881.     {
  2882.       size_t sz;
  2883.  
  2884.       sub->output_has_begun = false;
  2885.       sz = obj_raw_syment_count (sub);
  2886.       if (sz > max_sym_count)
  2887.     max_sym_count = sz;
  2888.     }
  2889.  
  2890.   /* Allocate some buffers used while linking.  */
  2891.   finfo.internal_syms = ((struct internal_syment *)
  2892.              bfd_malloc (max_sym_count
  2893.                      * sizeof (struct internal_syment)));
  2894.   finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
  2895.                          * sizeof (asection *));
  2896.   finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
  2897.   finfo.outsyms = ((bfd_byte *)
  2898.            bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
  2899.   finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
  2900.                        * bfd_coff_linesz (abfd));
  2901.   finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
  2902.   finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
  2903.   if (! info->relocateable)
  2904.     finfo.internal_relocs = ((struct internal_reloc *)
  2905.                  bfd_malloc (max_reloc_count
  2906.                      * sizeof (struct internal_reloc)));
  2907.   if ((finfo.internal_syms == NULL && max_sym_count > 0)
  2908.       || (finfo.sec_ptrs == NULL && max_sym_count > 0)
  2909.       || (finfo.sym_indices == NULL && max_sym_count > 0)
  2910.       || finfo.outsyms == NULL
  2911.       || (finfo.linenos == NULL && max_lineno_count > 0)
  2912.       || (finfo.contents == NULL && max_contents_size > 0)
  2913.       || (finfo.external_relocs == NULL && max_reloc_count > 0)
  2914.       || (! info->relocateable
  2915.       && finfo.internal_relocs == NULL
  2916.       && max_reloc_count > 0))
  2917.     goto error_return;
  2918.  
  2919.   /* We now know the position of everything in the file, except that
  2920.      we don't know the size of the symbol table and therefore we don't
  2921.      know where the string table starts.  We just build the string
  2922.      table in memory as we go along.  We process all the relocations
  2923.      for a single input file at once.  */
  2924.   obj_raw_syment_count (abfd) = 0;
  2925.  
  2926.   if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
  2927.     {
  2928.       if (! bfd_coff_start_final_link (abfd, info))
  2929.     goto error_return;
  2930.     }
  2931.  
  2932.   for (o = abfd->sections; o != NULL; o = o->next)
  2933.     {
  2934.       for (p = o->link_order_head; p != NULL; p = p->next)
  2935.     {
  2936.       if (p->type == bfd_indirect_link_order
  2937.           && (bfd_get_flavour (p->u.indirect.section->owner)
  2938.           == bfd_target_coff_flavour))
  2939.         {
  2940.           sub = p->u.indirect.section->owner;
  2941. #ifdef POWERPC_LE_PE
  2942.           if (! sub->output_has_begun && !ppc_do_last(sub))
  2943. #else
  2944.           if (! sub->output_has_begun)
  2945. #endif
  2946.         {
  2947.           if (! _bfd_coff_link_input_bfd (&finfo, sub))
  2948.             goto error_return;
  2949.           sub->output_has_begun = true;
  2950.         }
  2951.         }
  2952.       else if (p->type == bfd_section_reloc_link_order
  2953.            || p->type == bfd_symbol_reloc_link_order)
  2954.         {
  2955.           if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
  2956.         goto error_return;
  2957.         }
  2958.       else
  2959.         {
  2960.           if (! _bfd_default_link_order (abfd, info, o, p))
  2961.         goto error_return;
  2962.         }
  2963.     }
  2964.     }
  2965.  
  2966. #ifdef POWERPC_LE_PE
  2967.   {
  2968.     extern bfd* ppc_get_last();
  2969.     bfd* last_one = ppc_get_last();
  2970.     if (last_one)
  2971.       {
  2972.     if (! _bfd_coff_link_input_bfd (&finfo, last_one))
  2973.       goto error_return;
  2974.       }
  2975.     last_one->output_has_begun = true;
  2976.   }
  2977. #endif
  2978.  
  2979.   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
  2980.  
  2981.   coff_debug_merge_hash_table_free (&finfo.debug_merge);
  2982.   debug_merge_allocated = false;
  2983.  
  2984.   if (finfo.internal_syms != NULL)
  2985.     {
  2986.       free (finfo.internal_syms);
  2987.       finfo.internal_syms = NULL;
  2988.     }
  2989.   if (finfo.sec_ptrs != NULL)
  2990.     {
  2991.       free (finfo.sec_ptrs);
  2992.       finfo.sec_ptrs = NULL;
  2993.     }
  2994.   if (finfo.sym_indices != NULL)
  2995.     {
  2996.       free (finfo.sym_indices);
  2997.       finfo.sym_indices = NULL;
  2998.     }
  2999.   if (finfo.linenos != NULL)
  3000.     {
  3001.       free (finfo.linenos);
  3002.       finfo.linenos = NULL;
  3003.     }
  3004.   if (finfo.contents != NULL)
  3005.     {
  3006.       free (finfo.contents);
  3007.       finfo.contents = NULL;
  3008.     }
  3009.   if (finfo.external_relocs != NULL)
  3010.     {
  3011.       free (finfo.external_relocs);
  3012.       finfo.external_relocs = NULL;
  3013.     }
  3014.   if (finfo.internal_relocs != NULL)
  3015.     {
  3016.       free (finfo.internal_relocs);
  3017.       finfo.internal_relocs = NULL;
  3018.     }
  3019.  
  3020.   /* The value of the last C_FILE symbol is supposed to be the symbol
  3021.      index of the first external symbol.  Write it out again if
  3022.      necessary.  */
  3023.   if (finfo.last_file_index != -1
  3024.       && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
  3025.     {
  3026.       finfo.last_file.n_value = obj_raw_syment_count (abfd);
  3027.       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
  3028.                  (PTR) finfo.outsyms);
  3029.       if (bfd_seek (abfd,
  3030.             (obj_sym_filepos (abfd)
  3031.              + finfo.last_file_index * symesz),
  3032.             SEEK_SET) != 0
  3033.       || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
  3034.     return false;
  3035.     }
  3036.  
  3037.   /* Write out the global symbols.  */
  3038.   finfo.failed = false;
  3039.   coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
  3040.                (PTR) &finfo);
  3041.   if (finfo.failed)
  3042.     goto error_return;
  3043.  
  3044.   /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
  3045.   if (finfo.outsyms != NULL)
  3046.     {
  3047.       free (finfo.outsyms);
  3048.       finfo.outsyms = NULL;
  3049.     }
  3050.  
  3051.   if (info->relocateable)
  3052.     {
  3053.       /* Now that we have written out all the global symbols, we know
  3054.      the symbol indices to use for relocs against them, and we can
  3055.      finally write out the relocs.  */
  3056.       external_relocs = ((bfd_byte *)
  3057.              bfd_malloc (max_output_reloc_count * relsz));
  3058.       if (external_relocs == NULL)
  3059.     goto error_return;
  3060.  
  3061.       for (o = abfd->sections; o != NULL; o = o->next)
  3062.     {
  3063.       struct internal_reloc *irel;
  3064.       struct internal_reloc *irelend;
  3065.       struct coff_link_hash_entry **rel_hash;
  3066.       bfd_byte *erel;
  3067.  
  3068.       if (o->reloc_count == 0)
  3069.         continue;
  3070.  
  3071.       irel = finfo.section_info[o->target_index].relocs;
  3072.       irelend = irel + o->reloc_count;
  3073.       rel_hash = finfo.section_info[o->target_index].rel_hashes;
  3074.       erel = external_relocs;
  3075.       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
  3076.         {
  3077.           if (*rel_hash != NULL)
  3078.         {
  3079.           BFD_ASSERT ((*rel_hash)->indx >= 0);
  3080.           irel->r_symndx = (*rel_hash)->indx;
  3081.         }
  3082.           bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
  3083.         }
  3084.  
  3085.       if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
  3086.           || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
  3087.                 abfd) != relsz * o->reloc_count)
  3088.         goto error_return;
  3089.     }
  3090.  
  3091.       free (external_relocs);
  3092.       external_relocs = NULL;
  3093.     }
  3094.  
  3095.   /* Free up the section information.  */
  3096.   if (finfo.section_info != NULL)
  3097.     {
  3098.       unsigned int i;
  3099.  
  3100.       for (i = 0; i < abfd->section_count; i++)
  3101.     {
  3102.       if (finfo.section_info[i].relocs != NULL)
  3103.         free (finfo.section_info[i].relocs);
  3104.       if (finfo.section_info[i].rel_hashes != NULL)
  3105.         free (finfo.section_info[i].rel_hashes);
  3106.     }
  3107.       free (finfo.section_info);
  3108.       finfo.section_info = NULL;
  3109.     }
  3110.  
  3111.   /* If we have optimized stabs strings, output them.  */
  3112.   if (coff_hash_table (info)->stab_info != NULL)
  3113.     {
  3114.       if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
  3115.     return false;
  3116.     }
  3117.  
  3118.   /* Write out the string table.  */
  3119.   if (obj_raw_syment_count (abfd) != 0)
  3120.     {
  3121.       if (bfd_seek (abfd,
  3122.             (obj_sym_filepos (abfd)
  3123.              + obj_raw_syment_count (abfd) * symesz),
  3124.             SEEK_SET) != 0)
  3125.     return false;
  3126.  
  3127. #if STRING_SIZE_SIZE == 4
  3128.       bfd_h_put_32 (abfd,
  3129.             _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
  3130.             (bfd_byte *) strbuf);
  3131. #else
  3132.  #error Change bfd_h_put_32
  3133. #endif
  3134.  
  3135.       if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
  3136.     return false;
  3137.  
  3138.       if (! _bfd_stringtab_emit (abfd, finfo.strtab))
  3139.     return false;
  3140.     }
  3141.  
  3142.   _bfd_stringtab_free (finfo.strtab);
  3143.  
  3144.   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
  3145.      not try to write out the symbols.  */
  3146.   bfd_get_symcount (abfd) = 0;
  3147.  
  3148.   return true;
  3149.  
  3150.  error_return:
  3151.   if (debug_merge_allocated)
  3152.     coff_debug_merge_hash_table_free (&finfo.debug_merge);
  3153.   if (finfo.strtab != NULL)
  3154.     _bfd_stringtab_free (finfo.strtab);
  3155.   if (finfo.section_info != NULL)
  3156.     {
  3157.       unsigned int i;
  3158.  
  3159.       for (i = 0; i < abfd->section_count; i++)
  3160.     {
  3161.       if (finfo.section_info[i].relocs != NULL)
  3162.         free (finfo.section_info[i].relocs);
  3163.       if (finfo.section_info[i].rel_hashes != NULL)
  3164.         free (finfo.section_info[i].rel_hashes);
  3165.     }
  3166.       free (finfo.section_info);
  3167.     }
  3168.   if (finfo.internal_syms != NULL)
  3169.     free (finfo.internal_syms);
  3170.   if (finfo.sec_ptrs != NULL)
  3171.     free (finfo.sec_ptrs);
  3172.   if (finfo.sym_indices != NULL)
  3173.     free (finfo.sym_indices);
  3174.   if (finfo.outsyms != NULL)
  3175.     free (finfo.outsyms);
  3176.   if (finfo.linenos != NULL)
  3177.     free (finfo.linenos);
  3178.   if (finfo.contents != NULL)
  3179.     free (finfo.contents);
  3180.   if (finfo.external_relocs != NULL)
  3181.     free (finfo.external_relocs);
  3182.   if (finfo.internal_relocs != NULL)
  3183.     free (finfo.internal_relocs);
  3184.   if (external_relocs != NULL)
  3185.     free (external_relocs);
  3186.   return false;
  3187. }
  3188. #endif
  3189.  
  3190.  
  3191. /* The transfer vectors that lead the outside world to all of the above. */
  3192.  
  3193. #ifdef TARGET_LITTLE_SYM
  3194. const bfd_target
  3195. TARGET_LITTLE_SYM =
  3196. {
  3197.   TARGET_LITTLE_NAME,        /* name or coff-arm-little */
  3198.   bfd_target_coff_flavour,
  3199.   BFD_ENDIAN_LITTLE,        /* data byte order is little */
  3200.   BFD_ENDIAN_LITTLE,        /* header byte order is little */
  3201.  
  3202.   (HAS_RELOC | EXEC_P |        /* FIXME: object flags */
  3203.    HAS_LINENO | HAS_DEBUG |
  3204.    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  3205.   
  3206. #ifndef COFF_WITH_PE
  3207.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  3208. #else
  3209.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
  3210.    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
  3211. #endif
  3212.  
  3213.   0,                /* leading char */
  3214.   '/',                /* ar_pad_char */
  3215.   15,                /* ar_max_namelen??? FIXMEmgo */
  3216.  
  3217.   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  3218.   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  3219.   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  3220.  
  3221.   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  3222.   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  3223.   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  3224.   
  3225.   {_bfd_dummy_target, coff_object_p,     /* bfd_check_format */
  3226.      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
  3227.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  3228.      bfd_false},
  3229.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  3230.      _bfd_write_archive_contents, bfd_false},
  3231.   
  3232.   BFD_JUMP_TABLE_GENERIC (coff),
  3233.   BFD_JUMP_TABLE_COPY (coff),
  3234.   BFD_JUMP_TABLE_CORE (_bfd_nocore),
  3235.   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  3236.   BFD_JUMP_TABLE_SYMBOLS (coff),
  3237.   BFD_JUMP_TABLE_RELOCS (coff),
  3238.   BFD_JUMP_TABLE_WRITE (coff),
  3239.   BFD_JUMP_TABLE_LINK (coff),
  3240.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  3241.   
  3242.   COFF_SWAP_TABLE,
  3243. };
  3244. #endif
  3245.  
  3246. #ifdef TARGET_BIG_SYM
  3247. const bfd_target
  3248. TARGET_BIG_SYM =
  3249. {
  3250.   TARGET_BIG_NAME,
  3251.   bfd_target_coff_flavour,    
  3252.   BFD_ENDIAN_BIG,        /* data byte order is big */
  3253.   BFD_ENDIAN_BIG,        /* header byte order is big */
  3254.  
  3255.   (HAS_RELOC | EXEC_P |        /* FIXME: object flags */
  3256.    HAS_LINENO | HAS_DEBUG |
  3257.    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  3258.  
  3259. #ifndef COFF_WITH_PE
  3260.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  3261. #else
  3262.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
  3263.    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
  3264. #endif
  3265.  
  3266.   0,                /* leading char */
  3267.   '/',                /* ar_pad_char */
  3268.   15,                /* ar_max_namelen??? FIXMEmgo */
  3269.  
  3270.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  3271.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  3272.   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  3273.  
  3274.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  3275.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  3276.   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  3277.  
  3278.   {_bfd_dummy_target, coff_object_p,     /* bfd_check_format */
  3279.      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
  3280.   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  3281.      bfd_false},
  3282.   {bfd_false, coff_write_object_contents,    /* bfd_write_contents */
  3283.      _bfd_write_archive_contents, bfd_false},
  3284.  
  3285.   BFD_JUMP_TABLE_GENERIC (coff),
  3286.   BFD_JUMP_TABLE_COPY (coff),
  3287.   BFD_JUMP_TABLE_CORE (_bfd_nocore),
  3288.   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  3289.   BFD_JUMP_TABLE_SYMBOLS (coff),
  3290.   BFD_JUMP_TABLE_RELOCS (coff),
  3291.   BFD_JUMP_TABLE_WRITE (coff),
  3292.   BFD_JUMP_TABLE_LINK (coff),
  3293.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  3294.  
  3295.   COFF_SWAP_TABLE,
  3296. };
  3297.  
  3298. #endif
  3299.